Cache all the settings. You can get current theme from cache with the help of Singleton and Helper function.
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
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);
}
}
@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 :)
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community