Detailed composer_php instances of shared data and views between laravel views

Source: Internet
Author: User
1. Sharing data between views

In addition to passing the specified data in a single view, it is sometimes necessary to pass in the same data in all views, that is, we need to share the data in different views. To achieve this, you need to use the method of the View factory share .

The global help function is view response similar to returning an instance if an argument is passed in, and the instance is Illuminate\View\View returned without passing in the parameter Illuminate\View\Factory . So we can share data between views by using the following methods in the service provider's boot approach:

<?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 College ');  }  /**   * Register any application services.   *   * @return void   *  /Public Function register ()  {    //  }}

We routes.php define two routes in:

Route::get (' Testviewhello ', function () {  return view (' Hello ');}); Route::get (' Testviewhome ', function () {  return view (' Home ');});

Then resources/views create a view file under the directory home.blade.php , with the following content:

{{$sitename}} Home

Then create a hello.blade.php view file:

Welcome to {{$sitename}}!

The http://laravel.app:8000/testViewHello http://laravel.app:8000/testViewHome value that can be resolved by accessing and in the browser separately $sitename .

2. View composer

Sometimes we want to bind some specific data to the view each time the view renders, such as login user information, we need to use the view composer, view composer through the View Factory composer method implementation. The second callback parameter of the method supports both the controller action and the closure function.

For simplicity's sake, we're still based on AppServiceProvider , not going to create a service provider alone, here we pass the closure parameters (Controller action Reference 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 College ');    Views 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 ()  {    //  }}

To modify a hello.blade.php view file:

Welcome to {{$sitename}}!

User Information

User name: {{$user [' name ']}}
User picture: {{$user [' Avatar ']}}

Accessed in the browser http://laravel.app:8000/testViewHello , the output is as follows:

Welcome to Laravel College! User Information User name: Test user picture:/path/to/test.jpg

You can also pass 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 wildcard characters *):

View ()->composer (' * ', function ($view) {  $view->with (' user ', Array (' name ' = ' = ' Test ', ' avatar ' = ' = '/path /to/test.jpg ');});

The above is the Laravel view sharing data and view composer details, I hope this article on everyone learning laravel helpful.

  • Related Article

    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.