Laravel 5. Dynamic data in app.blade.php: BaseController or ViewComposers?

I have my packaging layout in app.blade.php, and for this I need dynamic data, say a list of categories displayed at the top of each view.

Following best practices, what would you do in Laravel 5?

  • To create a new service provider that downloads the new View Composer and sets the dynamic data for presentation in the compose () method (as a newbie, it looks like a lot of work for something so common)

  • To create a BaseController, set dynamic data for the view form and make each controller in my application to expand it.

I think this is a very common scenario. Imagine, for example, something like <h1>{{ pageTitle }}</h1>in app.blade.php that needs to be installed on each controller. But I could not find the right way to do this so far.

+4
source share
1 answer

A simple method just uses

view()->share('title', "My Page");

This works in your route.php files and several other files.

Using composer submission is also a great option.

0
source

All Articles