How to Return View from Controller in Laravel 5.2

In previous tutorial, I explained the different operations that a controller performs in Laravel. We also learned how to make hello world controller that return some text to user. In this post, I would explain how to make controllers that return view template. I would also explain how we can pass data to view templates from the controller.

In HelloWorldController, we looked at how controller can return short string to browser. In real applications, controller render response using views which are defined outside the controllers. These views are located at /resources/views folder of your Laravel application. After a controller has done its working e.g. interaction with model etc. Controller select a view and send its contents to browser. Remember, views are just PHP files that contains PHP, HTML, CSS etc.


To demonstrate above concepts, below I create UsersController.

C:\wamp\www\laraveltutorials>php artisan make:controller UsersController
Controller created successfully.

Here is our controller code located at /app/Http/controllers/UsersController with index method. Instead of returning short message itself, I select view inside the index method. The views' contents are sent to browser.

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;

class UsersController extends Controller
{
    public function index(){
        // do other processing here
        return view("users/home");
    }
}

You see, rather we return a string, we called a helper method view passing the name of view template to render. I passed users/home parameter to view method, controller loads the view templates by adding a prefix "resources/views" and adding a suffix "blade.php", the complete path would become "/resources/views/users/home.blade.php". Prefix and suffix are common for all views, so framework allow us to skip them and return only view name to keep the code clean.

You can also place home.blade.php file in /resoures/views folder too. But placing to many view templates in views folder makes it difficult to maintain and choose friendly file names for different controllers. So its better to make folders each for different controller and put out templates in that folder, it makes the application maintenance easier. The home.blade.php code is very simple, see below:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>How to Return Views from Controller in Laravel</title>
</head>
<body>

<h3>/users/home.blade.php file contents </h3>

</body>
</html>

Here is how user would access the controller and output displayed in browser:


In above case, we only selected view from controller but do not passed any contents to view to show users. In real application, we would definitely like to pass the data we retrieved from Model to view. So below sections explains how we can pass data to views.

How to Pass Data to View from Controller in Laravel

There are very few static pages in our website e.g. Contact Us, About Us etc. Most of the pages requires some data that is retrieved from database and displayed to user. So controller must be able to pass data (retrieved from model) to view templates.

In below controller, I have created page heading and list of users to pass both data items to view. One variables is string and other is array of strings. Definitely, we can pass any type of data using below approach. We pass data in the from of list of key values. The keys become available in our view template so that we can render it inside the view.

Lets see how our routing rule, controller and view would look like.

Write inside app/Http/routes.php file.

Route::get('/users/list', 'UsersController@listUsers');

The contents of App/Http/controllers/UsersController class

namespace App\Http\Controllers;

class UsersController extends Controller
{

    public function listUsers() {

        $userNames = array("Shahzad", "Amit", "Chris", "Zhang Wei", "Henry");
        $pageHeading = "Current Registered Users";

        return view("users/list", ["pageHeading" => $pageHeading, "names" => $userNames]);
    }
}

The contents of resources/views/users/list.blade.php

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>How to pass data to views in Laravel</title>
</head>
<body>

<h3> <?=$pageHeading ?></h3>
<ol>
    <?php foreach($names as $name) { ?>
        <li> <?=$name ?> </li>
    <?php } ?>
</ol>

</body>
</html>

Here is how the user would access the controller with response displayed to user.

Comments