FuelPHP - HMVC Request



FuelPHP provides an excellent feature to request an action inside the same application using Request class. This is called HMVC Request. It enables to reuse the controller logic.

Creating a HMVC Request

Creating a HMVC request is as simple as creating a request object with the required URL and call the execute method as follows.

$list = Request::forge('employee/list/')->execute(); 
echo $list;  

$employee = Request::forge('employee/show/1')->execute(array('id' => '1')); 
echo $employee;

Working Example

Let us create a new controller, Controller_HMVC to test the HMVC feature. Create a file, fuel/app/classes/controller/hmvc.php and place the following code.

<?php 
   class Controller_HMVC extends Controller { 
      public function action_index() { 
         echo Request::forge('employee/list')->execute(); 
      } 
   }

Here, we have just called the employee/list page through HMVC request and shown the result.

Result

Employee List
Advertisements