03. Controller

All controllers extend from, their parent, the abstract class ZController. Through the ZController they access the ZURI and the ZORM classes.

Creating a new controller

To create a new controller call the method create();

$this->create("new_controller");

This scaffolds the creation of a new engines folder with a controller.php file in it and a views folder.

Controller methods

The controller methods map to URIs in the order /controller/method. There are a few methods that are inherited from the ZController that are worth mentioning. These include:

render(), display(), dynamic()

These are described under views

redirect

This allows redirection following the 3 key parameters in the uri being conroller, method and id. eg:

self::redirect("foo"); //redirects to mysite.com/foo
self::redirect("foo", "bar"); //redirects to mysite.com/foo/bar
self::redirect("foo", "bar", 5); //redirects to mysite.com/foo/5
self::redirect("foo", "bar", "10/?action=delete"); //redirects to mysite.com/foo/10/?action=delete

The redirect also takes 2 integer arguments being 0 and 1 which allow a page to redirect on itself or return 1 step back in the history:

self::redirect(0); //redirects on itself as does self::redirect("self")
self::redirect(-1); //redirects to requesting/referring page as does self::redirect("back")

Finally the redirect method may be called without an argument and this redirects to the home page:

self::redirect(); //redirects to mysite.com/

The default controller

The engines folder comes with a default controller conveniently named 'default'. This folder ensures that the framework maps even if no other engines are defined.

mysite.com/ maps to mysqite.com/default/index

The index method

Every engine on extending the ZController inherits the index() method. This method takes care of situations where the controllers are accessed without explicitly including a method request in the url. This is the fall back for all controllers.

/contoller/ maps to /controller/index

ZURI

The ZURI class is a class that allows the controllers access url parameters by instantiating the ZURI class and calling any of its attibutes eg when on a URL like mysite.com/students/register/15/?subject=english+language&...:

$uri = new ZURI;
$uri->controller //maps to students 
$uri->method //maps to register 
$uri->id //maps to 15
$uri->arguments //maps every part of the url after the question mark (?) ie subject=english+language&...
$uri->gets //maps to $_GET that is an array array('subject'=>"english language", ...)
$uri->port //80
$uri->server //mysite.com
$uri->http //http://mysite.com:80
$uri->https //https://mysite.com:80

These become usefulin the controller as the the class does not need to be instantiated but my be accessed by the attibute $this->uri from all the controller instances.

ZORM

This is Zedeks in build Object Relational Mapper. It may be accessed from any controller using the attribute $this->orm Details of the mapper are explained in a later section.