Controllers

Controllers are classes that extends the base Qt_Controller, they are bound to URI through routes.

Below is an example of a basic controller class:


namespace Modules\Main\Controllers;

use Quantum\Mvc\Qt_Controller;

class MainController extends Qt_Controller 
{
    public function index() 
    {
        $this->setLayout('layouts/landing');
        $this->render('index');
    }
}
        

The index action under MainController will be executed when your route will have such defination


        array ('', 'GET', 'MainController', 'index')
        

Base Qt_Controller has several methods such as:

  • setLayout() - Sets the layout for page
  • render() - Renders the view but does not output unless view() function is called
  • output() - This method outputs rendered content immediately, useful for partial rendering
  • share() - Shares the data throughout layouts

Controllers has two special methods beside constructor

  • __before() - Runs before any action, if defined in controller
  • __after() - Runs after any action, if defined in controller

Parameters passed via GET request are available as arguments of an action. Consider the following route:


        array ('documentation/[:num]/[:any]', 'GET', 'MainController', 'show')
        

The method show() will receive two arguments according to above route


namespace Modules\Main\Controllers;

use Quantum\Mvc\Qt_Controller;

class MainController extends Qt_Controller 
{
    public function show($id, $slug) 
    {
        // The code goes here
    }
}
        

The request object can be injected into controller action at the first or the last possiton in method arguments if defined.


namespace Modules\Main\Controllers;

use Quantum\Mvc\Qt_Controller;

class MainController extends Qt_Controller 
{
    public function edit(Request $request, $slug) 
    {
        out($request->get('name'));
    }
}
        

To use the controller instance in views, there is a special helper function called qt_instance().

<< Prev Next >>