Table of Contents
Atomik action files do not follow any conventions. However, some of you may have used MVC frameworks where the business logic is coded in controllers. Controllers are classes where their methods are actions.
This plugin adds support for controllers to Atomik. Once activated you must use controllers in your actions. There is not possible mix between the classic way and the controller way.
There is two major differences which are views and the router.
Each controller have multiple actions (methods) and each action has its own view. While having
for example one file named user.php in the actions directory which
contains the UserController class (will come to that later), you'll need many view
files. Thus, instead of saving your views in the views directory you will have to save
them in a folder named after your action.
When using the router, the “action” parameter is mandatory. This plugin adds another mandatory parameter named “controller”. This parameter refers to the controller name whereas the “action” parameter refers to a method of the controller class.
The default route is “:controller/:action”.
Example 4.1. Creating routes
// ArchivesController::view()
Atomik::set('routes', array(
'archives/:year/:month' => array(
'controller' => 'archives',
'action' => 'view'
)
));
The default controller name is “index” and the default action name is “index”.
As said before, a controller is a class. The only condition is in the naming convention. Your class has to be named
using the controller name starting by an upper case letter suffixed with “Controller”. So for example,
with a controller named “user” (saved in app/actions/user.php), the class name
will be UserController.
Then add public method to your class. All public method which does not start with am underscore will be callable as an action.
Don't forget to also create the view associated to each action.
Example 4.2. Creating a simple controller
class UserController
{
public function index()
{
}
public function login()
{
}
}
Also create two view files: app/views/user/index.phtml and app/views/user/login.phtml.
Note that they are saved under the app/views/user directory where the last folder is the controller name.
You can then use the following urls: http://example.com/user or http://example.com/user/login.
In classic actions, all defined variables where accessible from the view. This is not possible anymore when using methods for evident reasons. To forward method to the view, simply define class properties.
Example 4.3. Creating a simple controller with view variables
class UserController
{
public function index()
{
$this->username = 'peter';
}
}
In your view (app/views/user/index.phtml):
hello <php echo $username ?>
Subclassing Atomik_Controller when creating a controller class brings some nice features.
First of all, you can define two methods _before() and _after()
that will be called before and after each action.
Secondly, route parameters will be automatically mapped to method arguments.
Example 4.4. Mapping route parameters to method arguments
Let's define the following route:
Atomik::set('routes', array(
'archives/:year/:month' => array(
'controller' => 'archives',
'action' => 'view'
)
));
And our controller:
class ArchivesController
{
public function view($year, $month)
{
}
}
The $year and $month argument will be taken from the route parameters.
Finally, it allows you to define routes parameters in the doc comment of your method. To do so, use the “@route” tag followed by the route definition.
Route define this way will have “:controller/:action/” automatically prepended.
When an argument is declared optional in the method, it will be declared optional in the route.
Example 4.5. Defining route parameters inside controllers
class ArchivesController
{
/**
* @route :year/:month
*/
public function view($year, $month)
{
}
}
This is the equivalent of the route defined in the previous example.