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.

C:\wamp\www\laraveltutorials>

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.

Comments

  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

    ReplyDelete
  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.
    Hadoop-Big-Data-Administration

    ReplyDelete
  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

    ReplyDelete
  4. This comment has been removed by the author.

    ReplyDelete
  5. After reading your post I understood that last week was with full of surprises and happiness for you. Congratz! Even though the website is work related, you can update small events in your life and share your happiness with us too.
    Click here:
    Microsoft azure training in annanagar
    Click here:
    Microsoft azure training in velarchery

    ReplyDelete

Post a Comment