Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5 view composer service provider, exclude prefix from wildcard

Tags:

php

laravel-5

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?

like image 611
Marijke Luttekes Avatar asked Feb 21 '15 14:02

Marijke Luttekes


1 Answers

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');
like image 61
lukasgeiter Avatar answered Nov 11 '22 17:11

lukasgeiter