Controller

The controller handles the main flow of the application and is responsible for returning a response to the client.

When a request is routed to a controller class, an instance of this controller is created and a suitable action method is invoked automatically.

Creating a controller class

To create a controller class, extend the BlueMvc\Core\Controller base class:

use BlueMvc\Core\Controller; class MyController extends Controller

Action methods

To handle a request, one or more action methods must be implemented in the controller.

Methods with public visibility, having a name that ends with Action are considered to be action methods.

There are three types of action methods:

Index action

public function indexAction()

The index action handles request for the index page, e.g. / or /foo/.

Custom action

public function barAction()

A custom action method will, in its basic form, handle requests for the page corresponding to the name preceding Action, e.g. /bar or /foo/bar.

Default action

public function defaultAction($action)

The default action handles requests where no other suitable action method was found.

The $action parameter is required and will contain a string with the action name.

Other useful methods

The controller contains some methods that can be useful when processing the request.

getApplication

$this->getApplication()

Returns the current application instance.

getRequest

$this->getRequest()

Returns the current request from the client.

getResponse

$this->getResponse()

Returns the response that will be sent to the client.

Pre- and post action event

The controller contains two methods that can be overridden to run code before and after an action method is invoked.

onPreActionEvent

protected function onPreActionEvent()

This method is invoked before the action method.

onPostActionEvent

protected function onPostActionEvent()

This method is invoked after the action method.

✎ Published in category core