Views in Laravel

Tram Ho

Hello friends. Next article in Laravel series today I will introduce View in Laravel.

1. Creating & Rendering Views

We can create a view by creating a file with .blade.php extension in the resources / views folder. The .blade.php extension will notify Laravel that the file contains a Blade template (which is the view engine in Laravel). The Blade template contains HTML and Blade commands allow us to easily iterate over values, generate if statements , loop over data, etc.When we have created a view, we can then return. Recall it from within a Laravel route or controller using the global view helper:

Views may also be rendered using the View facade:

The first argument will correspond to the file name we created in resources / views . The second argument is an array of data supplied to the view. As in the above example we are passing the name variable with the value James to the view greeting .

Nested View Directories

Views can also be nested in the resources / views subdirectory. “.” can be used to refer to views located in subfolders of views. For example, if your view file is stored at resource / views / admin / profile.blade , you can return it from within Laravel’s route or controller like so:

Determining If A View Exists

If you determine if a view exists, you can use the View facade and exists method to check the existence of the view:

Passing Data To Views

As you saw in the previous examples you can pass an array of values ​​to the view :

When passing data in this way, the data must be an array with key / value pairs. After supplying data to a view, you can access the data in the view using <? Php echo $ name; ?> . As an alternative to passing an array of data into a view, you can pass individual pieces into the view using the with method.

Sharing Data With All Views

Sometimes you need to display data with all laravel views. You may do this with the share method of the View facade. You should call the share method in the service provide boot method. You can add the App Providers AppServiceProvider class or create a separate provider to hold them:

2. View Composers

If you have a data that you want to associate with a view when that view is displayed, view composer can help you organize that logic into a single place. View composer is useful when your same view is returned by multiple routes or controllers and you always need to send a specific piece of data. Usually view composers will be registered in one of your provide services. In this example I will create a new ViewServiceProvider service provider to contain this logic:

We will use the composer method of the View facade to register the view composer for Laravel. Laravel will not include a default directory for view composers so you are free to arrange them however you want. In the above example I’m putting it in the app / Http / View / Composers directory . Now we will register a composer view, the compose method of the App Http View Composers ProfileComposer class will be executed every time the view profile is displayed. For example:

Attaching A Composer To Multiple Views

You can attach a composer view to multiple views at once by passing an array of views as the first argument to the composer method:

The composer method also accepts the * character as a wildcard, allowing you to attach view composer to all views:

3. Optimizing Views

By default, Blade template views are compiled on demand. When a request is made to request the rendering of a view, Laravel will determine if there exists a compiled version of the view. If the file already exists, Laravel will determine if the compiled view has been more recently modified than the compiled view. If the compiled view does not exist or the compiled view has been modified, Laravel will recompile the view. Compiling the views during the request process can have a small impact on performance. So Laravel provides the view: cache Artisan command to pre-compile all of the views used by your application. To increase performance, you may want to run this command as part of your code implementation:

You can use the view: clear command to clear the entire view cache:

Share the news now

Source : Viblo