FuelPHP - Controllers


Advertisements

Controllers are responsible for handling each request that comes into FuelPHP application. According to FuelPHP, controllers are located at fuel/app/classes/controller/. Let's first create an Employee Controller.

employee.php

<?php  
   class Controller_Employee extends Controller { 
      public function action_home() { 
         echo "FuelPHP-Employee application!"; 
      }  
      
      public function action_index() { 
         echo "This is the index method of employee controller"; 
      } 
   } 

Controller Methods

Controllers process a web request by using one of its action_ methods. We can create as many action_ methods depending on the requirement of the application. The default action_ method is action_index. action_index method can be called by any one of the following URLs.

http://localhost:8080/employee/index
http://localhost:8080/employee/

Result

Employee Controller

Let us create a new action method, action_show in our employee application.

<?php  
   class Controller_Employee extends Controller { 
      public function action_home() { 
         echo "FuelPHP-Employee application!"; 
      }  
      public function action_index() { 
         echo "This is the index method of employee controller"; 
      }  
      public function action_show() { 
         echo "This is the show method of employee controller"; 
      } 
   } 

action_show method can be called using the following URL.

http://localhost:8080/home/show

Result

Show Method

before( ) Method

We can create a method, before in our controller. This method will be executed before each and every action_ method invocation. It will not be called if that method turns out not to exist. This method helps us write common actions such as login checking, default data fetching, etc.

Let us create a before method and print a simple text message.

public function before() { 
   echo "This message comes from <em>before()</em> method</br>"; 
} 

Index page with before action

Index Before Action

Show page with before action

Show Before Action

after( ) Method

after() method is similar to before() method but executed after the action_ method is called. after() method takes response as an input and returns the response object.

public function after($response) { 
   if ( ! $response instanceof Response) { 
      $response = \Response::forge($response, $this->response_status); 
   } 
   return $response; 
} 

If the input is NULL or not response object, then create a new Response object using Response's forge method and return it. We will learn Response class in detail in the subsequent chapters.

Extending Controllers

We can extend one controller from another controller. Following is the basic syntax.

class Controller_Employee extends Controller_Welcome { 
   // controller methods 
} 

This will help in sharing methods.

Generate Controller

Fuel has the option to generate controller using Oil command. Following is the syntax.

Syntax

oil g controller <controller-name> 

Example

oil g controller sample

After executing the above command, you will see the following response.

Result

Creating view: /path/to/project/fuel/app/views/template.php 
Creating view: /path/to/project/fuel/app/views/sample/index.php 
Creating controller: /path/to/project/fuel/app/classes/controller/sample.php 

Type of Controllers

FuelPHP provides different type of controller for various purpose. They are as follows −

  • Base controller
  • Template controller
  • Rest controller
  • Hybrid controller

Base Controller

Controller is the base controller for all the different types of controllers available in FuelPHP. It provides all the basic functionalities needed to process a web request. It supports Request, Response, Session, etc. We will be using it in all examples unless otherwise specified.

Template Controller

A Template Controller is an extension of the base controller. It has template support, predefined before() and after() methods. Basically, it can be used to wrap your view in a layout with a header, footer, sidebar, etc. To create a template controller, we need to extend the Controller_Template class. By default, all methods of a class that extends Controller_Template need to use the template.

It is defined as follows.

class Controller_Employee extends Controller_Template { 
   public function action_index() { 
      // add methods 
   } 
}

We will discuss more about template controller in Views chapter.

Rest Controller

A Rest Controller is an extension of the Base Controller. It has pre-defined support for REST API programming. This will allow you to build APIs with ease.

To create rest controller, you need to extend the Controller_Rest class. It is defined as follows.

class Controller_Employee extends Controller_Rest { 
   public function action_index() { 
      // add methods 
   } 
}

We will discuss more about rest controller in Ajax chapter.

Hybrid Controller

The hybrid controller performs the functionality of both the REST controller and the Template controller in a single base controller.

Advertisements