Documentation Index
Fetch the complete documentation index at: https://kawax.biz/llms.txt
Use this file to discover all available pages before exploring further.
Introduction
Returning an entire HTML document as a string from a route or controller is not practical. Views let you keep all your HTML in separate files. Views separate your controller and application logic from your presentation logic. Laravel stores views in theresources/views directory. By convention, Laravel views are written using the Blade templating language.
resources/views/greeting.blade.php and you can return it using the global view helper.
Creating views
To create a view, place a file with a.blade.php extension in the resources/views directory, or use the Artisan command:
.blade.php extension tells the framework that the file contains a Blade template.
Returning views
Once you have created a view, you can return it from a route or controller using the globalview helper:
View facade:
view corresponds to the name of the view file in the resources/views directory. The second argument is an array of data to make available to the view.
Nested view directories
Views can be nested inside subdirectories ofresources/views. Use dot notation to reference nested views.
For example, a view stored at resources/views/admin/profile.blade.php can be returned like this:
Returning the first existing view
Use thefirst method on the View facade to return the first view that exists from an array:
Checking if a view exists
Use theexists method on the View facade to check whether a view exists:
Passing data to views
As shown in the examples above, you pass an array of data to a view to make it available inside:with method. It returns the view instance, so you can chain calls:
Sharing data with all views
To share data with every view in your application, use theshare method on the View facade. Call it in the boot method of a service provider:
Optimizing views
By default, Blade templates are compiled on demand. When a request renders a view, Laravel checks whether a compiled version exists and whether the template has been modified. You can pre-compile all views using theview:cache Artisan command — this is recommended as part of your deployment process:
Next steps
Blade templates
Learn how to use Blade syntax to build dynamic views.