Laravel Views

In model view controller MVC frameworks view are the interfaces that display output as well as accept input from user. These view are complementary component of any web application. In most of the php frameworks views are created using .html or .php file extension. In contrast laravel provide a default tempaleting engine.

Laravel Blade Template

In laravel view/interfaces have .blade.php extension. Where .blade prefix in file extension specify that that is laravel template file and will be rendered using laravel template engine. These blade files are stored in resources/views directory. We can also create here more sub folders/directories according to project/application requirements.

Laravel Auth Directory

In laravel there is a default sub directory for containing blade files related to the authentication process. There authentication directory may include register, login, reset password files. We can also create here more view files but it is not recommended.

Admin and User Layout Files

In laravel it is recommended to separate each domain blade files into different folders. For example to handle the interfaces of admin user we have to create separate directory such as resources/views/admin. Similarly for the user specific files there will be a specific directory. We can also manage more sub directories for other domains such as invoices, payment modules , dialogs, options, dynamic contents. In such a way it is more easy to handle large size projects. This type of directory structure is also called nested view directories.

Nested View Structure in Laravel 10

Return View From Route File

We can render .blade file from anywhere means in particular from the following files which may include.

  • Route
  • Controller
  • Model

It is recommend to return views from model file containing data values. In some cases it is required to return view from the route directly without calling any controller routine. For handle this type of situation we can use the following .

Syntax

Return view from Route

Route::get('/', function () {
    return view('welcome', ['data' => 'value']);
});

We can also use View facade for the similar task. In route it is not mandatory to use View facade.

Return view from Model

For returning blade files eg views from model or controller we are required to use View facade. The syntax of returning views from model is as below.

use Illuminate\Support\Facades\View;

Syntax

return View::make('welcome', ['data' => 'value']);

We can notice here View facade is used/included in model class from where .blade file will be returned. In latest versions of Laravel above 4.0 we can include this facade by using simple "use" keyword.

Example

use View;

Syntax of using Views

Views can be returned using make function of View facade. This method accept two parameters, where first parameters is the name of blade file that would be rendered and second argument is the data/data array which can be accessed in specified view. It is common practice to send data values along with view files. The syntax of rendering view is as.

Syntax

return View::make('welcome');

How to pass data in views

We can send single value or array of data to the view file. These data values are easily accessible inside views. We can also specify other identifier to the data variables. In this way data can be shared in between multiple view dynamically. These data variables can also update in the final view files.

Syntax

return View::make('welcome', ['data' => 'value']);

In the said example we are returning welcome blade file from model using make function of View facade. As discussed earlier the second parameter is data array. Here data is the identifier and used with $data variable in welcome view. This variable will contain "value" as data. In similar way multiple data values can be sent which will be the part of data array.

Access nested Views

To access nested directory views we have to specify folder name before blade file name. There is no limit for nesting of directories. But it is recommended to use minimum levels of folders. In this technique blade file is separated by slash or dot operator. Each sub directory from resources/views folder will be accessed using dot operator.

Syntax

return View::make('admin.welcome', ['data' => 'value']);

Pass array of views

In laravel we can also pass array of views as first argument while rendering blade files. This syntax is more likely used with fitst function of View facade.  This technique may useful in certain web applications.

Syntax

return View::first(['dynamic.user', 'user'], ['data' => 'value']);

How to share data among all blade files

The question is that is there any option for sharing universal data value among all views rendering in entire application. So the answer is surely laravel provide such a technique. For this purpose we have to use share method of View facade. An passed the data value or array of data in boot method of service provider class. Alternatively we can also create a separate service provider file to perform this task.

Example

public function boot(): void
    {
        View::share('data', 'SomeValue');
    }

 

Comments
Login to TRACK of Comments.