Home >>Laravel Tutorial >Laravel Views

Laravel Views

Views in Laravel

Views in web development is the structure to be displayed on web browser. It contains all HTML code which is used to display the content of the website i.e. front-end. Laravel views are seperated from the controller and the models of the MVC structure of laravel.
The location of views is inside resources folder.

[project_folder]/resourse/views
Given below is an example showing the views concept in Laravel.
Here we're creating a about us view of a website where we'll be displaying about structure of the website.
  • Step 1 :Create a view file naming About.blade.php in the resourse/views folder.
  • <html>
    <body>
    <h1>This is About Page of <?php echo $name?></h1>
    </body>
    </html>
    
  • Step 2 : Now add on routing for this page to get access i.e. adding about url in web.php
  • Route::get('/about', function()
    {  
      return view('About',['name'=>'PHPTPOINT']);  
    });  
    
    Here, view() is a method which has two arguments. One is the name of file containing the view, and other with an array passed to pass a variable to the view file.
  • Step 3 :Run this URL on web browser
  • http://127.0.0.1/about
    you'll be displayed as the view you've created.
Output:
about view in laravel

Controller Class View Method

So far we returned a view which is simple just for the knowledge of view how it shows up. Let's now create a view which works with the view() method in controller class.

  • Step 1 : Create a Controller naming 'ViewController' (Go ahead and check controllers in Laravel here) Inside your ViewController.php file create a function for returning view.
  • public function show()
    {  
      return view('about');  
    }  
    
  • Step 2 : Now create a file about.php in the resourse/views folder as created in above example.
  • <html>
        <body>  
            <h1>About Us Page using Controller</h1>
        </body>
    </html> 
    </html>  
    
  • Step 3 : Finally add the url you wish to name along with the Controller and the method you creted.
  • Route::get('/show','ViewController@show');
    Here, you can see the '/show' is the url which on access runs 'ViewController' and then the 'show' method is then run inside the ViewController
  • Step 4 : Run the URL on browser for displaying View:
  • http://127.0.0.1/show
Output:
view in laravel

Nested Views

There can be views inside the sub-directory in resourse/views directory.

  • Step 1 : Create details.blade.php file inside a folder like admin and paste code in details.blade.php
  • <html>  
        <body>
            <h1>Details View inside Admin Folder</h1> 
        </body>  
    </html>
    
  • Step 2 : Make ShowController.php as done above and inside controller just make a show() method returning the 'admin.details'
  • public function show()
    {  
      return view('admin.details');  
    }  
    
  • Step 3 : Add route in web.php file to make url.
  • Route::get('/details', 'ShowController@show');
    
  • Step 4: Run the URL
  • http://127.0.0.1/details
Output:
Nested view in laravel

Note:

In Laravel view exists or not is determined by using view facades. exists() method returns true if view exists.

So in the above example we can check this thing by adding following conditions inside ShowController.php to check view existance.

use Illuminate\Support\Facades\View;
public function show(){  
  if(View::exists('admin.details'))
  {
	echo "the View of admin.details exist";
  }
  else
  {
  echo "View does not exist";
  }
}  

Here, View::exists('admin.details') determines that admin.details exist or not.


No Sidebar ads