This version is outdated. You should upgrade your project to Mako 12.1!
Controllers

Request



The task of the request class is to gather information about the current request and route requests to the appropriate action.

An instance of the request object is always available in controllers and it can be accessed like this:

$this->request

Usage

You can request a controller action within another controller action using subrequests.

$response = Request::factory('controller/action')->execute();

The phpcontroller method returns the name of the requested controller.

$controllerName = $this->request->controller();

The phpaction method returns the name of the requested controller.

$actionName = $this->request->action();

The phpip method returns the ip address of the client who made the request.

$ip = $this->request->ip();

The phpreferer method returns the URL of the referer.

$referer = $this->request->referer();

The phproute method returns the route of the request.

$route = $this->request->route();

The phpmethod method returns the request method that was used.

if($this->request->method() == 'POST')
{
	// Do something
}

The phpheader method returns a HTTP header.

$header = $this->request->header('user-agent');

// You can also specify a default return value if the header isn't set

$header = $this->request->header('foo-bar', 'default value');

The phpusername method returns the basic HTTP authentication username or NULL.

$username = $this->request->username();

The phppassword method returns the basic HTTP authentication password or NULL.

$password = $this->request->password();

The phpisAjax method returns TRUE if the request was made using AJAX and FALSE it not.

if($this->request->isAjax())
{
	// Do something
}

The phpisSecure method returns TRUE if the request was made using HTTPS and FALSE it not.

if($this->request->isSecure())
{
	// Do something
}

The phpisMain method returns TRUE this is is the main request and FALSE if its a subrequest.

if($this->request->isMain())
{
	// Do something
}

The phpmain method returns an instance of the main request.

$request = Request::main();

Routing

All routing configuration is done in the phpapp/config/routes.php file.

'custom_routes' => array
(
	'hello' => 'foo/bar',
),

If a user visits phphttp://example.org/hello then the request will be routed to phpfoo/bar and the phpbar action of the phpfoo controller will be executed.

Routing can also be a good way of prettifying your URLs. This example shows how you can hide the index action from your users.

'custom_routes' => array
(
	'product/([0-9]+)' => 'product/index/$1',
),

Visiting phphttp://example.org/product/10 will now be the same as if a user went to phphttp://example.org/product/index/10.

You can define as many custom routes as you want but note that they will be checked in the order that they are defined.