Following the view composer documentation I've created a class ComposerServiceProvider and a ViewComposer for my basic views. I want to use another view composer for the administration area of my site called AdminComposer. These are my class headers:
(namespace App\Http\Composers)
class ViewComposer
class AdminComposer extends ViewComposer
This is my Composer Service provider:
<?php namespace App\Providers;
use View;
use Illuminate\Support\ServiceProvider;
class ComposerServiceProvider extends ServiceProvider {
/**
* Register bindings in the container.
*
* @return void
*/
public function boot()
{
View::composer('admin/*', 'App\Http\Composers\AdminComposer');
View::composer('*', 'App\Http\Composers\ViewComposer');
}
/**
* Register any application services.
*
* @return void
*/
public function register()
{
//
}
}
In the method boot() of my composer service provider I want to assign the ViewComposer to all my views, except for the ones with the prefix "admin", which should use AdminComposer.
In the current setting however, both view composers are used, as path "admin/*" adheres to path "*/" as well. Is there a way I can exclude the admin prefix from the wildcard path, without having to individually declare all paths that will use ViewComposer instead?
It's probably enough to only attach the view composers to your two layout files. This way you not only resolve the conflict between normal and admin views but also the composer runs just once per request and not twice or more (for the view and the layout view and possibly more views)
View::composer('layouts.admin', 'App\Http\Composers\AdminComposer');
View::composer('layouts.master', 'App\Http\Composers\ViewComposer');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With