Create a view
Application view contains the HTML code, and the separation performance of the controller logic and logic applications. View files stored in the resources/views
directory. Here is a simple example of view:
<!-- 该视图存放 resources/views/greeting.php -->
<html>
<body>
<h1>Hello, {{ $name }}</h1>
</body>
</html>
Because of this view is stored in resources/views/greeting.php
, we can assist function view
so that it returns like:
Route::get('/', function () { return view('greeting', ['name' => '张三']); });
As you can see, is transmitted to the view
first argument is the resources/views
name of the corresponding file directory view, the second parameter is an array that contains all valid data in this view. In this example, the transfer of a name
variable, by using the view Blade syntax to display it.
Of course, the view can also be stored in resources/views
a subdirectory, with the number to refer to nested view, for example, if the view is a storage path. "" resources/views/admin/profile.blade.php
, Then we can refer to it:
return view('admin.profile', $data);
Determining whether there are views
If you need to determine whether there are views that can be called View
on the facade of the exists
method, if the view in the presence of the disk is returned true
:
use Illuminate\Support\Facades\View; if (View::exists('emails.customer')) { // }
Creating the first valid view
Using a view first
to create a view to the first view given array present method. This is useful when your application or extension package allows custom or cover view:
return view()->first(['custom.admin', 'admin'], $data);
Of course, you can also call View
on the facade first
ways to create:
use Illuminate\Support\Facades\View; return View::first(['custom.admin', 'admin'], $data);
Data passed to the view
Can be seen in the above example, you can simply pass the data to the view through the array by:
return View ( 'Greetings', [ 'name' => 'John Doe']);
Transmitting data in this manner, it $data
should be an array of key-value pairs, in a view, corresponding keys can be used to access data values, for example <?php echo $key; ?>
. In addition, also by with
adding a separate piece of data to the view method:
$view = view('greeting')->with('name', '张三');
View sharing data between
Sometimes, we need to share data across all segments views, this time the view of the facade can use the share
method, generally, need a service provider boot
calls the method share
method, you can add it to AppServiceProvider
or generated by independent service providers logical person to hold this code:
? < PHP namespace App \ Providers; use View; class AppServiceProvider the extends ServiceProvider { / * * * Start all application services * * @return void * / public function the Boot () { View :: report this content share ( 'Key', 'value' ) ; } / * * * registered service provider * * @return void * / public function Register () { // } }
View Composer
View Composer when a callback function or class method when the view is rendered. If you have some data to view every time when rendering do binding, you can use the View Composer will logically organized into a single place.
In this example, the first to be registered in a service provider in view of the Composer , we will use View
the facade to access Illuminate\Contracts\View\Factory
the underlying implementation, remember, Laravel will not include the default view Composer catalog, we can organize it according to their own preferences path, for example, you can create a app/Http/View/Composers
table of contents:
? < PHP namespace App \ Providers; use Illuminate \ Support \ Facades \ View; use Illuminate \ Support \ ServiceProvider; class ComposerServiceProvider the extends ServiceProvider { / * * . * Registration is bound vessel * * @return void * @author HTTP: //laravelacademy.org * / public function Boot () { // based composers class methods ... View :: Composer ( 'Profile', 'the App \ the Http \ ViewComposers \ ProfileComposer' ); // based callback the composers ... Composer :: View ( 'Dashboard', function ( $ View ) {}); } / * * . * Registered service provider * * @return void * / public function Register () { // } }
Note: If you create a new service provider to contain the view registration Composer, you need to add the service provider to the profile
config/app.php
ofproviders
the array.
Now that we have registered the view Composer , each profile
will perform when rendering the view ProfileComposer@compose
method, then we define the Composer class:
<? PHP namespace App \ Http \ ViewComposers; use Illuminate \ View \ View; use Illuminate \ Repositories \ UserRepository; class ProfileComposer { / * * . * Users warehouse implementation * * @var UserRepository * / protected $ the Users ; / * * * Create a new property Composer * * @param Users UserRepository $ * @return void * / public function the __construct (UserRepository $ Users ) { // dependency injection is automatically analyzed by the service container ... $ the this -> Users =Users $ ; } / * * * Binding data to the view. * * @param View View $ * @return void * / public function Compose (View $ View ) { $ View -> with ( 'COUNT', $ the this -> USERS-> COUNT ()); } }
Front view is rendered, Composer class compose
method is invoked, while Illuminate\View\View
examples of the method are injected, which can be used with
a method to bind to the data view.
Note: All View Composer are resolved through the service container, so you can declare any dependencies you need in the constructor Composer class.
Was added to a plurality of views Composer
You can view the array as passed composer
to the view method of disposable first parameter Composer added to the plurality of views:
View::composer( ['profile', 'dashboard'], 'App\Http\ViewComposers\MyViewComposer' );
composer
The method also supports *
wildcards, thereby allowing all views to add a Composer:
View::composer('*', function ($view) { // });
View Builder
View Builder and View Composer are very similar, except that the former fail immediately rather than waiting until after the view is about to render a view instantiation. Use View
the facade of the creator
method to register a view of the creator:
View::creator('profile', 'App\Http\ViewCreators\ProfileCreator');