Laravel: Difference between View::share() and View::composer()

10,454

Solution 1

Technically they are not at all alike. View::share simply sets a variable, while View::composer is a callback function.

Let me explain in greater detail:

View::share is really straight forward it sets a variable which can be used within any of the views, think of it like a global variable.

View::composer registers an event which is called when the view is rendered, don't confuse it with a View::creator which is fired when a view is instantiated.

View::composer / View::creator can both be used as a class which is well documented.

While these give you the ability to pass additional data to a view, they also give you to ability to do a lot of other things, for example they could:

  • Aid in debugging a view
  • Log information about views
  • Be used to create custom caching (probably not a great idea, but possible)

These are just some examples of what could be possible using View::composer and View::creator.

Solution 2

View::composer('*', callback());

Means that the callback will be called for all views (*).

View::share

Means that a variable will be shared with all outputed views.

Because the first is in filters.php, it'll apply for all routes.

The second is in a controller contructor, so it'll apply for all views triggered by this controller.

One last thing: when overriding a constructor, it's a good pratice to allways call the parent constructor with this code:

parent::_construct();
Share:
10,454
enchance
Author by

enchance

Laravel, Wordpress, HTML, CSS, SCSS, Twig, and JS http://kaltencoder.com

Updated on July 23, 2022

Comments

  • enchance
    enchance almost 2 years

    In relation to the question Passing default variables to view, to pass variables available among all views, is there a technical or functional difference between the use of View::composer():

    View::composer('*', function($view) {
        $thundercats = 'Woooooohh!!';
        $view->with('thundercats', $thundercats);
    })
    

    in the filters.php file or the use of View::share() in the BaseController.php file:

    public function __construct {
        $thundercats = 'Woooooohh!!';
        View::share('thundercats', $thundercats);
    }
    

    I've only recently learned about View::share() and find it exceptionally intruiging although I've already started using the former in another project.

    Edit:

    My first assumption is that the former is a file (filters.php) while the the latter is a class (BaseController.php). With this in mind, I'm guessing a class is much better? Although, I'm not quite sure why at this point. :)

  • enchance
    enchance over 10 years
    I see. so Views::composer variables can be accessible even in routes.php and any other file? Out of curiousity, can this be done to make a specific variable available for all controllers other than $this->varname?
  • enchance
    enchance over 10 years
    I see. So for simplicity's sake, if all you want is to output a 'global' variable then it's better to use View::share(). For anything other/more than that, use View::composer().
  • tplaner
    tplaner over 10 years
    For simplicity's sake sure, however if you find yourself defining it over and over again probably abstract it to a View::composer.
  • Christopher Raymond
    Christopher Raymond over 9 years
    If View::composer('*', callback()) is a valid option, it should be in the documentation.
  • Carlton
    Carlton almost 8 years
    Also check out this answer stackoverflow.com/a/23857517/682754. Both really helped me understand the difference.