How does Laravel assign values to public templates
This article focuses on "how Laravel assigns values to public templates". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how Laravel assigns values to public templates.
Many times during the development process, values are assigned to common templates, such as the top navigation bar, the bottom of the page, etc., and it is impossible to assign values in every controller.
The solution in Laravel is as follows: modify
App\ Providers\ AppServiceProvider
Add in the boot method
View ()-> composer ('common.header',function ($view) {/ / common.header corresponds to Blade template $view- > with (' key', 'value');})
You can also assign values to all templates
View ()-> share ('key',' value')
View composers is related to the view and is used in a boot () function of service provider, that is, when a view is loaded, due to the role of view composer, to call a function to pass a parameter or something.
1, create service provider
Php artisan make:provider ComposerServiceProvider
Then add ComposerServiceProvider to config/app.php.
2, write view composer
Public function boot () {view ()-> composer ('app', / / template name' App\ Http\ ViewComposers\ MovieComposer' / / method name or method in class);}
Once app.blade.php is loaded, execute the composer function in App\ Http\ ViewComposers\ MovieComposer (the composer function is the default here), and if you want to change it,
View ()-> composer ('app','App\ Http\ ViewComposers\ MovieComposer@foobar'); / / self-defined method
This is where the foobar function is executed
Write this in App\ Http\ ViewComposers\ MovieComposer.php