Detailed Laravel shared data and views composer_php instances between views

Source: Internet
Author: User
Tags closure

1. Share data between views

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

Global help functions view and response similar, if passed in arguments, returns Illuminate\View\View an instance and returns an instance without passing in the argument Illuminate\View\Factory . So we can boot implement shared data between views by using the method of the service provider as follows:

<?php

namespace App\providers;

Use Illuminate\support\serviceprovider;

Class Appserviceprovider extends serviceprovider
{
  /**
   * Bootstrap any application services.
   *
   * the @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 table home.blade.php of contents as follows:

{{$sitename}} Home

To create a second hello.blade.php view file:

Welcome to {{$sitename}}!

The http://laravel.app:8000/testViewHello http://laravel.app:8000/testViewHome values that can be parsed if they are accessed separately in the browser $sitename .

2, view composer

Sometimes we want to bind certain data to the view, such as login user information, every time the view is rendered, we will use the view composer, the view composer through the composer method of the View factory. The second callback parameter of the method supports two methods, based on the controller action and the closure function.

For simplicity's sake, we're still basing ourselves on AppServiceProvider not creating a separate service provider , where we pass the closure parameters (Controller action Reference video document):

<?php

namespace App\providers;

Use Illuminate\support\serviceprovider;

Class Appserviceprovider extends serviceprovider
{
  /**
   * Bootstrap any application services.
   *
   * the @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}}!

 
 

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

Welcome to Laravel College! User

information

user name: Test
user head:/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 wildcards *):

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

The above is Laravel view shared data and view composer details, I hope this article for everyone to learn laravel help.

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.