Laravel-Backpack / Settings

Application settings interface for Backpack (for Laravel 6).
http://backpackforlaravel.com
Other
248 stars 78 forks source link

Settings don't register in the console #79

Closed Patabugen closed 5 years ago

Patabugen commented 5 years ago

Bug report

Settings don't get loaded when running console apps. This is giving me problems with my Functional Testing.

I'd be interested to know why CLI is excluded, and whether it can be enabled?

What I did:

Setup as instructed, everything is working. I then tried to run FunctionalTests using PHPBrowser (via CodeCeption).

What I expected to happen:

For my settings to be available in all versions of the app.

What happened:

The settings don't get loaded because Backpack\Settings\SettingsServiceProvider::boot() specifically excludes loading them when running on the command line.

What I've already tried to fix it:

I've changed: if (!\App::runningInConsole() && count(Schema::getColumnListing('settings'))) { to if (count(Schema::getColumnListing('settings'))) {

(removing !\App::runningInConsole() &&) and it's now working as expected.

Backpack, Laravel, PHP, DB version:

Settings: 2.1.2 Backpack CRUD: 3.5.10 Laravel: 5.7.16 PHP: 7.2

welcome[bot] commented 5 years ago

Hello there! Thanks for opening your first issue on this repo!

Just a heads-up: Here at Backpack we use Github Issues only for tracking bugs. Talk about new features is also acceptable. This helps a lot in keeping our focus on improving Backpack. If you issue is not a bug/feature, please help us out by closing the issue yourself and posting in the appropriate medium (see below). If you're not sure where it fits, it's ok, a community member will probably reply to help you with that.

Backpack communication mediums:

Please keep in mind Backpack offers no official / paid support. Whatever help you receive here, on Gitter, Slack or Stackoverflow is thanks to our awesome awesome community members, who give up some of their time to help their peers. If you want to join our community, just start pitching in. We take pride in being a welcoming bunch.

Thank you!

-- Justin Case The Backpack Robot

Patabugen commented 5 years ago

I've just seen this has already been discussed, and potentially fixed (though I'm not getting it yet).

https://github.com/Laravel-Backpack/Settings/issues?utf8=%E2%9C%93&q=is%3Aissue+console https://github.com/Laravel-Backpack/Settings/pull/70 https://github.com/Laravel-Backpack/Settings/pull/19

So closing this, as a dupe.

Patabugen commented 5 years ago

For anyone else coming here looking for the answer - you need to this:

Add this to each file using Settings: use Backpack\Settings\app\Models\Setting;

Then use this: Setting::get('my-setting-name'); instead of config('settings.my-setting-name');

and it'll work everywhere.

Personally I've just copied this bit from the Service Provider and put it into my own AppServiceProvider:


    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        $this->loadSettings();
    }

    /**
     * The default Backpack Settings module does not work on the Command Line
     * because it fails if you don't have a database. Since we do have a
     * database it's safe for us to use it. This code is copied from the
     * Backpack\Settings\SettingsServiceProvider::boot() method with the
     * runningInConsole() conditional removed.
     *
     * @return void
     */
    public function loadSettings()
    {
        if (count(Schema::getColumnListing('settings'))) {
            // get all settings from the database
            $settings = Setting::all();

            // bind all settings to the Laravel config, so you can call them like
            // Config::get('settings.contact_email')
            foreach ($settings as $key => $setting) {
                Config::set('settings.'.$setting->key, $setting->value);
            }
        }
    }
tabacitu commented 5 years ago

@Patabugen thanks for sharing your solution with everyone! I bet someone will be very grateful to find your post.

Actually, you don't really need the use statement. This package's service provider already adds the Setting alias to Laravel's default configuration. So you should be able to use Setting::get() reliably inside you app anywhere, without having to explicitly use the class. This is way better than using Config::get('setting.your_key') so I'll update the docs right now to put this method as the default one.

Thanks again, cheers!

caveman99 commented 3 years ago

@tabacitu i found out today while accessing settings values in a homemade artsan command, that i had to explicitly "use Backpack\Settings\app\Models\Setting". Seems Laravel 8 does not do auto-namespaces any more.

pxpm commented 3 years ago

Hello there @caveman99

It's totally expected outcome. config() is part of app instance itself and is not available on console commands, for that you'd need to use Config::get('your_key')

Best, Pedro