Support the ongoing development of Laravel.io →
Blade Views Architecture
Last updated by @ajax30 2 years ago.

mostafa-amine liked this thread

1

Cache all the settings. You can get current theme from cache with the help of Singleton and Helper function.

0

There are many ways to achieve that. You can use View Share if you have to share that with all the views.

But I personally prefer to define these settings in the config/app.php file and create a settings_middleware in which I will set the value for these settings each time a route is invoked that is assigned the settings_middleware.

vdvcoder liked this reply

1
moderator

Another way is to not need the config everywhere.

You can define a package view directory, see: https://laravel.com/docs/9.x/packages#views
In a package provider you could add the path:

public function boot()
{
    // from a config file as @faisal mentioned.
    $this->loadViewsFrom(config('app.theme_directory'), 'theme');
}

And in controller you get:

return view('theme::templates/index', ['articles' => $articles]);

If you dive in the code you see that this is a function on the View Factory

That means that if you really need to get it from the database you can make a serviceprovider or middleware that does the database call. I should personal use a middleware so you only need to execute the database call for routes that need it.

use Illuminate\Contracts\View\Factory;

class ThemeSettingsMiddleware
{
    public function __construct(private Factory $viewFactory)
    {
    }
    public function handle($request, Closure $next)
    {
        $this->viewFactory->loadViewsFrom(Settings::first()->theme_directory, 'theme');
        return return $next($request);
    }
}
0

@faisal Can we improve this blogging app?

0
moderator

@ajax30 people will give an reaction on your other thread if they want and have time. So it is not needed to point them to it from this thread :)

0

Sign in to participate in this thread!

Eventy

Your banner here too?

Razvan ajax30 Joined 2 Oct 2021

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.