Introduction
Returning entire HTML documents as strings directly from routes and controllers is impractical. Views allow you to place all of your HTML in separate files. Views separate your controller / application logic from your presentation logic and are stored in theresources/views directory.
In Laravel, views are typically written using the Blade templating language.
resources/views/greeting.blade.php and can be returned using the global view helper.
Creating views
To create a view, place a file with the.blade.php extension in the resources/views directory, or use an Artisan command.
.blade.php extension tells the framework that the file contains a Blade template.
Returning views
Once a view is created, you can return it from a route or controller using the globalview helper.
View facade:
view helper corresponds to the name of the view file in the resources/views directory.
The second argument is an array of data that should be made available to the view.
Nested view directories
Views may be nested within subdirectories ofresources/views.
“Dot” notation is used to reference nested views.
For example, if your view is stored at resources/views/admin/profile.blade.php, you can return it like so:
Returning the first available view
Using thefirst method of the View facade, you can return the first view that exists from a given array of views.
Checking if a view exists
To check if a view exists, use theexists method on the View facade.
Passing data to views
As you saw in the previous examples, you can pass an array of data to views and use that data within the view.with method.
The with method returns an instance of the view object, so you can chain method calls.
Sharing data with all views
If you want to share data with every view, use theshare method on the View facade.
You typically place this in the boot method of a service provider.
Optimizing views
By default, Blade template views are compiled on demand. Because view compilation runs during the request, this may have a slight performance impact. Theview:cache Artisan command lets you pre-compile all of the views used by your application.
It’s recommended to run it as part of your deployment process.
Next steps
Blade templates
Learn how to use Blade syntax to build dynamic views.