At the very end of Views section of docs there's a mention of creators, maybe they will work as you want:
View creators work almost exactly like view composers; however, they are fired immediately when the view is instantiated. To register a view creator, use the creator method:
View::creator('profile', 'App\Http\ViewCreators\ProfileCreator');
Spot on, thanks Xum!
Looking at the source code, the two functions set up events, 'creating:' and 'composing:'
$composers[] = $this->addViewEvent($view, $callback, 'composing: ', $priority);
I guess this code is run as part of the view lifecycle.
It's a shame the naming is not particularly intuitive - creator()
sounds like it would do something completely different, such as create a view. This is strange, as I know one of Tylor Otwell's aims is to provide readable / obvious method names.
It seems to be this would be more consistently implemented (as per controller filters) as View::beforeFilter()
and View::afterFilter()
rather than View::create()
and View::compose()
.
Anyway, thanks for the pointer.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community