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