Laravel 5.2 Hello World Controller Example

Lets first see what a controller is and what task it performs, then I would make a simple controller that prints hello world message to browser.

Controller is core component in MVC architecture. A controller in Laravel perform three major tasks:
  1. Controller interpret the request i.e. extracting request and URL route parameters
  2. Controller interact with application persistence state, called Model. Model is not limited to database; interacting with caches, indexing servers, directory servers, file store, or even third party APIs is all part of persistence state related operations. Controller do not perform these operations itself, it calls other software module or components e.g. services or data access objects to communicate with persistence state.
  3. Based on URL, request & route parameters and results returned by Model, controller renders response to user. Controller render the response using one of these approaches:
    1. Controller passes data retrieved from model to view template and renders view template i.e. a PHP file, to user.
    2. Instead of dispatching request to PHP file to render view, Controller can return the response text directly. This normally happen for AJAX based requests or when we use controllers to make RESTful services. (I would cover how to use AJAX and RESTful services using Laravel in separate posts).
    3. Controller may redirect browser to another URL. For example, AccountsController can redirect browser to DashboardController after creating new user account.
Once the Laravel has identified a controller using request URL pattern using rules defined in routes.php file, it transfers the request processing task to Controller. I hope you got the basic idea of what controller does, now I explain how to create first controller. Then I define mapping for the controller and explain how above mentioned stuff can be achieved programmatically.

Create Empty Controller

There is very helpful command line tool called Artisan that comes with Laravel, which contains many methods to perform different tasks. Instead of creating class in out project, we can use Artisan command to create a controller for us. Point your CLI console to your project root and issue the following command to create HelloWorldController

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


After this command, you can see HelloWorldController.php class placed in app\Http\controller folder having following contents.

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;

class HelloWorldController extends Controller

Defining index Method in HelloWorldController

You see there is no method in the controller above by default. We must define methods inside controller to actually process the requests. Lets define and index method inside the controller that returns a string directly to user browser. Then we must define mapping in routes.php file to map /helloworld URL pattern to index method inside HelloWorldController. Here is the update controller code:

class HelloWorldController extends Controller
    function index(){
        return "HelloWorldController index method called.";        

Lets define the mapping in routes.php file so that when users enters /helloworld in the browser, Laravel dispatch the request processing to HelloWorldController's index method.

Route::get('/helloworld', 'HelloWorldController@index');

Its self explanatory, we have just defined rule that ask Laravel to route GET request on /helloworld URL patterns to index method of HelloWorldController class. Here is how user would enter URL and what response user would get:

If you are done with making basic controller that return a string to user. Now you should see how controller can return view using separate view templates and how we can pass data to view from controller.


  1. Thanks for splitting your comprehension with us. It’s really useful to me & I hope it helps the people who in need of this vital information.
    PHP Institutes in Chennai|PHP Training Center in Chennai

  2. Thanks for splitting your comprehension with us. It’s really useful to me & I hope it helps the people who in need of this vital information.

  3. Thanks for sharing this informative content that guided me to know the details about the training offered in different technology.
    msbi training in chennai

  4. Wonderful blog.. Thanks for sharing informative blog.. its very useful to me..

    iOS Training in Chennai

  5. Wonderful blog.. Thanks for sharing informative blog.. its very useful to me..

    iOS Training in Chennai

  6. Thanks for providing a valuable information with us. get an offer for MSBI Online Training Hyderabad

  7. Nice post about MSBI, looking for best msbi online training institute ?

  8. This comment has been removed by the author.


Post a Comment

Popular posts from this blog

Task 12 - Create Java Program with MySQL Database Connectivity

Java Interfaces and Exception Handling - Practice Problem with Solution

Task 16 - Javafx Program to Create, Retrieve, Update and Delete records with Database