Controllers

Controllers are the brains of Sodapop applications. They are the starting point for processing requests, and are responsible for passing information to the models and views.

An Example

Controllers in Sodapop are subclasses of Sodapop_Controller. They are (by default) placed in the application's /controllers directory.

The standard routing determines the contoller (and action) based on the page's URL. 1

http://example-domain.com/post/edit/slug/this-is-my-post-slug
  • post: the controller name, in this example PostController.
  • edit: the action name, in this example ActionEdit.
  • slug/this-is-my-post-slug: a request variable, the equivalent of ?slug=this-is-my-post-slug

This URL could be handled with the following controller:

The Request Cycle

The request process for a Sodapop application follows the following steps:

  1. Using mod_rewrite, the web server directs the request to the index.php in the application's www directory.
  2. The Application object is instantiated and its run() method is called. This method reads the configuration files.
  3. The request is compared to the routes.json file and if a match is found, the controller and action to be invoked are determined from there. Otherwise, they are chosen based on the default routing. 2
  4. The application's loadControllerAction() method is called.
    1. If the requested controller or action is not found, the action404() method of the IndexController is called. 3
    2. The controller's preDispatch() method is called.
    3. The action method is called.
    4. The controller's postDispatch() method is called.
    5. The view/layout is rendered, if the controller's render() method has not already been called.
    6. The controller's cleanup() method is called.
    7. The output is echoed to the client.
  5. Execution completes.

Developers can override the preDispatch(), postDispatch() and cleanup() methods to perform necessary actions at the appropriate points in the request process.

Public Methods

action*() (void)
Methods that start with the word "action" are available to be called as URLs.
action404() (void)
Method that is called if the action or controller is invalid.
action500() (void)
Method that is called if there is an uncaught exception during execution.
actionIndex() (void)
The default action for the controller. Called if the controller is invoked without an action.
cleanup() (void)
A method that executes after the page has been rendered but before it is sent to the client. Meant to be overridden if necessary.
postDispatch() (void)
A method that executes after the action. Meant to be overridden if necessary.
preDispatch() (void)
A method that executes before the action. Meant to be overridden if necessary.
redirect($url) (void)
Stops execution and sends a server-side redirect to the client. The supplied URL can be either absolute or relative.
setHeader($header, $value) (void)
Sets an HTTP header.

Properties

These properties are available via the $this scope within the controller.

$action (string)
The action called.
$controller (string)
The controller called.
$layoutFile (string)
The name of the layout to be used. Defaults to "layout". 4
$request (Sodapop_Request)
The Request object for this request.
$view (Sodapop_View_Abstract)
The view object. Properties can be assigned to it that will become available when the view and layout templates render.
$viewPath (string)
The view template to be used, in the format "controller/action". It will default to the correct value for the executing action. 5