Detailed description of shared data and views between Laravel views Composer and laravelcomposer
1. share data between views
In addition to passing specified data in a single view, sometimes the same data needs to be passed in all views, that is, we need to share data in different views. To achieve this, you need to use the view Factoryshare
Method.
Global Help Functionsview
Andresponse
Similarly, if a parameter is input, returnIlluminate\View\View
Instance. If no parameter is input, returnIlluminate\View\Factory
Instance. Therefore, we canboot
Use the following method to share data between views:
<? Phpnamespace App \ Providers; use Illuminate \ Support \ ServiceProvider; class AppServiceProvider extends ServiceProvider {/*** Bootstrap any application services. ** @ return void */public function boot () {// view shared data view ()-> share ('sitename', 'laravel ');} /*** Register any application services. ** @ return void */public function register (){//}}
Inroutes.php
Two routes are defined in:
Route::get('testViewHello',function(){ return view('hello');});Route::get('testViewHome',function(){ return view('home');});
Thenresources/views
Createhome.blade.php
View File with the following content:
{$ Sitename} Homepage
Create anotherhello.blade.php
View File:
Welcome to {$ sitename }}!
Access in the browser separatelyhttp://laravel.app:8000/testViewHello
Andhttp://laravel.app:8000/testViewHome
, Can be parsed$sitename
.
2. View Composer
Sometimes we want to bind some specific data to the view during each view rendering, such as logging on to the user information. In this case, we need to use the view Composer. The view Composer is implemented through the view factory composer method. The second callback parameter of this method supports two methods: controller action and closure function.
For simplicity, we are still based onAppServiceProvider
, Not separately createdService ProviderHere we pass the closure parameters (for details about the Controller action, refer to the view document ):
<? Phpnamespace App \ Providers; use Illuminate \ Support \ ServiceProvider; class AppServiceProvider extends ServiceProvider {/*** Bootstrap any application services. ** @ return void */public function boot () {// view shared data view ()-> share ('sitename', 'laravel '); // view Composer view ()-> composer ('hello', function ($ view) {$ view-> with ('user ', array ('name' => 'test', 'Avatar '=>'/path/to/test.jpg ') ;}/ *** Register any application services. ** @ return void */public function register (){//}}
Modifyhello.blade.php
View File:
Welcome to {$ sitename }}!
<H3> user Information
Access in a browserhttp://laravel.app:8000/testViewHello
, The output content is as follows:
Welcome to Laravel college! User Information Username: test user profile:/path/to/test.jpg
You can also transmit data to multiple views:
view()->composer(['hello','home'],function($view){ $view->with('user',array('name'=>'test','avatar'=>'/path/to/test.jpg'));});
Even all views (using wildcards *):
view()->composer('*',function($view){ $view->with('user',array('name'=>'test','avatar'=>'/path/to/test.jpg'));});
The above is the details about shared data and Composer views between Laravel views. I hope this article will help you learn about Laravel.