Controllers


Description


The task of a controller is to receive input and decide what to do with it (e.g, fetch an article from a model, assign the data to a view and then return the view).

There is a one-to-one relationship between a URL and the controller action that gets executed. Visiting http://example.org/foo/bar will normally execute the bar action of the foo controller. This behaviour can be changed by using custom routes.

Controllers are instantiated by the Request class. The instance of the request object is accessed using the $this->request property. The second property that is always available in a controller is an instance of the Response class that can be accessed using $this->response.


All controllers must be located in the app/controllers directory and extend the mako\Controller class.

Hello, world!


If you save the following code as example.php and go to http://example.org/example then you'll be greeted by Hello, world!.


<?php

namespace app\controllers;

class Example extends \mako\Controller
{
	public function action_index()
	{
		return "Hello, world!";
	}

	public function action_date()
	{
		return date('l jS \of F Y h:i:s A');
	}
}

Visiting http://example.org/example/date will display the current date. You might have noticed that both actions are prefixed with action_. This lets you define actions with the same name as reserved keywords in PHP (e.g, 'list' would not work while 'action_list' would).

Passing arguments to a controller action is easy as you can se in the example below.


<?php

namespace app\controllers;

class Arguments extends \mako\Controller
{
	public function action_color1($color)
	{
		return 'your favorite color is ' . $color;
	}

	public function action_color2($color = 'green')
	{
		return 'your favorite color is ' . $color;
	}
}

Visiting http://example.org/arguments/color1/blue will display my favorite color is blue. Going to http://example.org/arguments/color1 will display a 404 error. This is because the argument is required. The second action has an optional argument so visting http://example.org/arguments/color2 will not produce an error but instead tell you that your favorite color is green.


Special methods


action_index


The framework will try to execute the action_index method if a controller is requested without specifying which action to run.


before


The before method is always executed before the requested action.


after


The after method is always executed after the requested action.


__construct


You should not override the parent constructor. The before method can be used instead.