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.