Laravel 8 Tutorial - Laravel Views
Published January 29, 2022We use views in Laravel 8 to retrieve the entire HTML document strings from the controllers and routes. Views allow us to separate the application logic from the presentation logic, and they are stored in the resources/views directory. In this chapter, we will look at:
-
How to create and render a View
-
How to Create a Nested View Directories
-
How to Pass Data to Views
-
How to communicate data to other Views
1. How to Create and render a View
We usually create a view by saving a file with the a.blade.php extension in the resources/views directory. This extension plays a vital role in informing the Laravel 8 framework, which file contains the Blade template. Blade templates include all the files with the HTML and the blade directives, which enable you to echo values or create if statements.
A view comprises the HTML content of a Laravel application. The following code snippet represents a Laravel view
<html> <body> <h1>Hello, {{ $name }}</h1> </body> </html>
|
With the global view helper, you can return your view from your application controller or route.
Example
Route::get('/', function () { return view('welcome', ['name' => 'Homepage']); }); |
Alternatively, you can return the view using the View façade.
Example
use Illuminate\Support\Facades\View; return View::make('welcome', ['name' => 'Homepage']); |
To pass your view successfully, you need to ensure that the first argument that has been passed to the view helper corresponds to the view file in the resources/view directory.
2. How to Create a Nested View Directories
You can also have your views nested within the subdirectories within the resources/views directory. To create a nested view, you only have to use the “dot” notation.
For instance, if your view is stored in the resources/views/ students/marks.blade.php, then you can opt to return it from the application controllers/routes
return view('students.marks', $data); |
3. How to Pass Data to Views
You can make specific data available to views by passing that data array. To pass the data to the view, you will first need to convert the data into the form of an array with a key or value pair.
For example, to pass the student marks to the view, we can pass them from an array and display them in the views
return view('student', ['marks' => 'CHEM100%']); |
To display this information in the view
<?php echo $marks; ?> |
4. How to communicate data to other Views
You may need to share certain data with other views displayed by your application from time to time. To do this, we use the view façade's share function. In this way, you must insert the calls to share the method inside the service provider's boot method.
You may either include them in the App\Providers\AppServiceProvider or create additional service providers to host them.
5. How to Optimise Views
Blade template views are typically compiled whenever they are required. This implies that when a view-rendering request is performed, it decides which view exists. If the view is discovered to be uncompiled, it must be updated more recently than the compiled view. In the absence of an updated compiled view, Laravel will be forced to recompile the view.
Consequently, view compilation during the request has minimal effect on performance. Laravel includes the view: cache Artisan command for recompiling views that have recently been used in the application. To improve your application view, just execute the following command
PHP artisan view: cache |
Conclusion
That concludes the Laravel Views Chapter. We learned how to construct views, nested viewing directories, send data to views, and share information with other views
Article Contributed By :
|
|
|
|
302 Views |