Laravel Views - VIews and Responses Tutorial | W3school

Views

By now, you must have understood the importance of Views. In fact, the MVC framework itself emphasizes the importance of V as Views. In simple words, a view acts as a bridge between application logic and the presentation logic. You can locate this bridge i.e., views within the resources/views directory. Well generally, views are HTML codes which work as an application.

Also Read: Laravel Response

For better understanding, you can take a look at the below example.
Step 1: Save the following code at the resources/views path. You can name this file as test.php.

<html>
<head>
<title> Test View </title>
</head>

<body>
<h1> <?php echo $name; ?> </h1>
</body>
</html>

[post_middile_section_ad]

Step 2: Now find routes.php at the path app/Http. You will be required to add the following line of code in the file. This will help in setting up an appropriate route for the recently created view.

Route::get(‘/test’, function()
{
return view(‘test’,[‘name’=>’Virat Gandhi’]);
});

Step 3: As mentioned in the code, the name ‘Virat Gandhi’ is then passed into the variable ‘name’. The $name variable of the test.php will later be replaced by ‘Virat Gandhi’ whenever the view gets invoked.

Step 4: Now, paste the URL: http://localhost:8000/test in the PHP’s web server. You will get an output which will appear like the following.

Sharing Data with All Views
As for now, you have understood to pass arguments in a ‘view’. Well, there are other views too where you don’t need to pass data. But, to share data with all the views, Laravel offers you a simple way out. The MVC framework offers share() method for this purpose. The method is capable of accepting 2 arguments which are key and value. Generally, this method is to be called from the boot() method of the service provider such as AppServiceProvider or any of your own service provider.

Here is the example.
Step 1: Copy the given lines of code in the routes.php file. You can locate this file in app/Http.

Route::get(‘/test’, function()
{
return view(‘test’);
});

Route::get(‘/test2’, function()
{
return view(‘test2’);
});

Step 2: Now, just to test, we will be sharing the data with two views. For this, you are required to create 2 files with the name test.php and test2.php. After this, include the following HTML codes in both the files. These files should be saved under the directory: resources/views/.

<html>
<body>
<h1> <?php echo $name; ?> </h1>
</body>
</html>

Step 3: Now, the boot() method of the service provider requires a few changes. To change the code, follow the path: app/Providers/. Here, you will get a file with the name, AppServiceProvider.php. In this file, we will be using share() method inside the boot() method. This passed data will then be available to all the shared views.

<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
// Bootstrap any application services @return void
public function boot()
{
view()->share(‘name’, ‘Virat Gandhi’);
}

// Register any application services. @return void
public function register()
{ // }
}

Step 4: Now, to test this share() method. You need to type in the 2 URLs in the web browser. These URLs are URL1: http://localhost:8000/test and URL2: http://localhost:8000/test2. Later, you will be displayed with an output like the following.

Blade Templates
A blade is a powerful templating engine provided by Laravel. It is usually considered as the lightweight template language of Laravel. Even, its syntax is quite simple and easy to learn. It has an extension by the name blade.php. You can locate this file at resources/views/.

Another, an important feature of the blade template is that it supports all the major constructs of PHP which include @for, @foreach, @while, @if, and @elseif. This feature is useful for the creation of loops and conditions in a program. With the help of blade template, you can even avoid opening and closing tags i.e., </php and ?>. But one of the major use of this template is to set up a master template which is capable of getting extended by other individual HTML pages.

Below is the example.
Step 1: Start with the creation of a master template. For this, copy the below given code to the master.blade.php file. You can find this file at resources/views/layouts/.

<html>
<head>
<title> @yield(‘title’)</title>
</head>
 
<body>
@section(‘sidebar’)
This is the master sidebar.
@show
 
<div class = “container”>
@yield(‘content‘)
</div>
</body>
</html>

Now, before we proceed further, understand the use @yield(‘title’), @section(‘sidebar’), @show and @yield(‘content’).

* @yield(‘title’) – displays the value of the title
* @section(‘sidebar’) – defines sidebar which is just a section
* @show – displays the contents present in a section
* @yield(‘content’) – displays all the contents present within a content

[post_middile_section_ad]

Step 2: At this point, you are required to create another page with the name page.blade.php. This is to extend the master template. The newly created page should be stored under resources/views/.

@extends(‘layouts.master’)
@section(‘title’, ‘Page Title’)
 
@section(‘sidebar’)
@parent
<p> This is appended to the master sidebar. </p>
@endsection
 
@section(‘content‘)
<h2> {{$name}} </h2>
<p> This is my body content. </p>
@endsection

Now, dissecting each element of the code for your better understanding.

* @extends(‘layouts.master’) – helps in extending the master layout (or template).

* ‘layouts.master’ – master is a template which denotes ‘master.blade.php’ and is stored under the layouts directory.

* @section(‘title’,’Page Title’) – sets the value of the ‘title’ as ‘Page Title’.

* @section(‘sidebar’) – defines a section named as the sidebar. This is placed on the child page of the master template.

* @parent – displays the defined content of sidebar section which can be found under the master template.

* <p> – denotes the starting of a paragraph.

* @endsection – stands for the ending of the section ‘sidebar’.

* @section(‘content’) – defines the section with the name ‘content’ and adds a paragraph to this section.

* @endsection – displays the end of the section – ‘content’.

Step 3: To view this template, you are required to set up a route. For this, add the following lines of code in routes.php file. You can find this file in app/Http.

Route::get(‘blade’, function ()
{
return view(‘page’,array(‘name’ => ‘Virat Gandhi’));
});

Step 4: Copy the given URL: http://localhost:8000/blade to the PHP’s web server and you will get the following output.