Controllers

Controllers are the driving force of a PIP application and are stored in the "app/controller" directory.

RESTful Routing

PIP uses search-engine and human friendly URLs, and automatically routes to the required controller using a segment-based approach:

example.com/controller/action/parameter

Pip adds a suffix of Controller to the controller classname, and it prefixes the action with get or post, depending on the type of request.

So the following Url:

example.com/album/show/65

would route to class AlbumController (in file app/controllers/album.php) and call the function getShow with a parameter value of 65.

app/controllers/album.php
<?php

class AlbumController extends Controller {

    function getIndex()
    {
        // This is the default function (ie. no function in the URL)
    }
    
    function getShow( $param )
    {
        // show album with id = param (65) 
    }
}
?>

Constructors

Note that if you need to declare a contructor you must also call the parent constructor like:

<?php

class ExampleController extends Controller {

    public function __construct()
    {
        parent::__construct();
        // Your own constructor code
    }
}
?>

Loading

There are several helper functions that can also be used in controllers. Most of these functions take the parameter $name of the corresponding class:

Note: Also see the configuration file config['register'] option to pre-load models, helpers, etc.

    loadModel($name);       // Load a model
    loadView($name);        // Load a view
    loadPlugin($name);      // Load a plugin
    loadHelper($name);      // Load a helper

Redirects

The redirect function has been added to helpers/Url. The original controller function redirect($location) (redirect to a page without having to include the base URL) has been retained for backward compatibily.

$this->redirect('some_class/some_function');