elliotk
131
2
Laravel

Laravel Architecture Howto

Posted 8 months ago by elliotk

I am a bit stuck and need some architecture advice.

I am using the following theme package in my app, which allows users to set a theme of their choice.

https://github.com/igaster/laravel-theme

At the moment, the theme choice is set via a Settings model. This and other settings are retrieved and cached via the AppServiceProvider. All this is working fine.

    public function boot()
    {   
        $settings = \Cache::rememberForever('settings', function () {
                return Setting::all();
            });

        if (isset($settings->theme)) {
            if (Theme::exists($settings->theme)) {
                Theme::set($settings->theme);
            } else {
                Theme::set('default');
            }
        }
    }

However, I came across an issue installing the code for a new domain. Long story short, i believe that my app wouldn't install via Forge because the above was executing, when the migrations hadn't run, and the base tables were not present.

I then decided to move the above to a View composer instead, which allowed the app to install fine. However, i have now found, that the Theme doesn't return the correct (override) views, I presume because the view is already passed to the composer, by which point it's too late to alter it via the Theme setting.


    public function boot()
    {   

        View::composer(['*'], function ($view) {
            $settings = \Cache::rememberForever('settings', function () {
                return Setting::getAll();
            });

            if (isset($settings->theme)) {
                if (Theme::exists($settings->theme)) {
                    Theme::set($settings->theme);
                } else {
                    Theme::set('default');
                }
            }

            $view->with('settings', $settings);
        });
}

So my question is, how can I retrieve the users preferred theme from a Model, and pass it to the Theme package, without using a View Composer, but also in a way that allows migrations to run on a fresh install.

Thanks for any advice.

Elliot

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.