Difference between View Composer and Creator in Laravel?

后端 未结 3 1300
既然无缘
既然无缘 2020-12-24 12:11

According to Laravel 4 documentation.

Composer is:

View composers are callbacks or class methods that are called when a view

3条回答
  •  不知归路
    2020-12-24 12:57

    It took me a while to work this out, I had to dig in the source code to work it out. The difference is at what point in the cycle of the Laravel application you want the command to run.

    There are a number of points in the Laravel cycle involving views.

    You can make a view using View::make(). This is when a view is instantiated - and during the View::make() command any View::creators() are called, before the function is returned.

    Normally you just run return View::make() - which means the view is 'created', and then returned to the Laravel core where it is then 'composed' to screen. This is when the View::composer() is called (i.e. after the view has returned).

    I'm not sure why you would want to use one or the other, but that explains the difference between the two.

提交回复
热议问题