Detailed description of shared data and views between Laravel views Composer and laravelcomposer

Source: Internet
Author: User

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 FactoryshareMethod.

Global Help FunctionsviewAndresponseSimilarly, if a parameter is input, returnIlluminate\View\ViewInstance. If no parameter is input, returnIlluminate\View\FactoryInstance. Therefore, we canbootUse 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.phpTwo routes are defined in:

Route::get('testViewHello',function(){  return view('hello');});Route::get('testViewHome',function(){  return view('home');});

Thenresources/viewsCreatehome.blade.phpView File with the following content:

{$ Sitename} Homepage

Create anotherhello.blade.phpView File:

Welcome to {$ sitename }}!

Access in the browser separatelyhttp://laravel.app:8000/testViewHelloAndhttp://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.phpView 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.