What are views in Laravel

admin_img Posted By Bajarangi soft , Posted On 18-01-2021

Laravel is an MVC based PHP framework. In MVC architecture, V stands for View. The View is data that is going to be displayed to the user on their browser and the user can interact with it. It is simply an interface provided to the user for interaction Laravel used a powerful templating engine called Blade. The extension for this file used here is filename.blade.php. Even though there are some directives and layout format used with a blade for taking advantage of this templating engine, still it allows us to write plain PHP in the view file.

views in Laravel

The main advantage of using Blade Templating Engine is that it provides template inheritance. With this, we can create a master layout page, which will become the base for other pages. So, the master page becomes the parent for all the child pages that we extend it to. IN this article we will cover the below topics:

  • Check for View Existence
  • Create the First Available View
  • Create and Display Data in a View
  • Share Data with all Views
  • View Composer
  • View Creator

1. Check for View Existence: To check if the view exists, there is a method called ‘exists()’.

Syntax
View::exists('gfg');

In place of ‘gfg’, you can specify the name of the view that you want to see it exists or not. This will output ‘true’ if the view exists and ‘false’ otherwise
Example:
Create a view in ‘resources/views’ directory with the name ‘gfg.balde.php’. Write the below code in it.

<!DOCTYPE html> 
<html> 
<head> 
    <title>GeeksforGeeks</title> 
</head> 
<body> 
    <h3>This is my View</h3> 
</body> 
</html>
Create a controller with the name GfGController using the following command ‘php artisan make:controller GfGController’ and write the below code in it.
Note: The View facade use Illuminate\Support\Facades\View; should be defined in the controller file.
<?php 

namespace App\Http\Controllers; 

use Illuminate\Http\Request; 
use Illuminate\Support\Facades\View; 

class GfGController extends Controller 
{ 
    public function index() { 
        if (View::exists('gfg')) { 
            return view('gfg'); 
        } 
        else { 
            return 'No such view exist.'; 
        } 
    } 
} 
?> 
Write the code below in ‘web.php’ file in ‘route’ directory.

Route::get('/', 'GfGController@index');
Note: Comment or remove any previously defined routes.

2. Create the First Available View If there are many views in a Laravel application then using the ‘first()’ method, we can create the view that is available first.
Syntax

view()->first(['main', 'gfg', 'article']);
View::first(['main', 'gfg', 'article']);

Note: For this syntax, you will need to specify the View facade use Illuminate\Support\Facades\View; in the controller file.

Create a view in resources/viewsgfg.balde.php. Write the below code in it.
Syntax
View::exists('gfg');<!DOCTYPE html> 
<html> 
<head> 
    <title>GeeksforGeeks</title> 
</head> 
<body> 
    <h3>This is the First Available View</h3> 
</body> 
</html>
Create a controller with the name GfGController using the following command php artisan make:controller GfGController and write the below code in it.
Note: The View facade use Illuminate\Support\Facades\View; should be defined in the controller file.

Syntax
<?php 

namespace App\Http\Controllers; 

use Illuminate\Http\Request; 
use Illuminate\Support\Facades\View; 

class GfGController extends Controller 
{ 
    public function index() { 
        return View::first(['main', 'gfg', 'article']); 
    } 
} 
?> 
Write the code below in web.php file in route directory.
Note: Comment or remove any previously defined routes.

Syntax
Route::get('/', 'GfGController@index');
3. Create and Display Data in a View: To display any kind of data stored in a variable then Blade provides a way to do that and that is by using ‘{{ }}’ i.e. double curly braces. The statements here are sent through PHP’s htmlspecialchars function which does the conversion of special characters to HTML entities. This is done to prevent XSS (Cross Site Scripting) attacks and this is done automatically by Blade.
Syntax
My name is {{ $name }} and my age is {{ $age }}
The variable $name and $age will be replaced by the values stored in those variables. If we don’t want the data to be escaped then we can use {!! !!}. This will prevent the data from being escaped but now it is not being protected by XSS attacks.
Syntax
My name is {!! $name !!} and my age is {!! $age !!}
Syntax
My name is {!! $name !!} and my age is {!! $age !!}
Example:Write the below code in web.php file in routes directory.
Route::get('/', function () {
  return view('gfg')->with('name' => 'Aakash')->with('age' => '21');
}
Now, let’s create a view file gfg.blade.php in resources/views directory with the name specified in the route file in the previous step and write the below code in it.
Example
<!DOCTYPE html> 
<html> 
<head> 
    <title>GeeksforGeeks</title> 
</head> 
<body> 
    <h3>My name is {{ $name }} and my age is {{ $age }}</h3> 
</body> 
</html>

Related Post