@@ -10,19 +10,14 @@ actions.
1010This plugin adds support for controllers to Atomik. Once activated you must use controllers in your
1111actions. It is not possible to mix between the classic way and the controller way.
1212
13- The plugin will be disabled when a pluggable application starts. It can be enabled using
14-
15- Atomik\Controller::$disable = false;
16-
1713## Differences with the classic Atomik way
1814
1915There are two major differences which are views and the router.
2016
2117Each controller have multiple actions (methods) and each action has its own view. While having
22- for example one file named * user.php* in the * actions* directory which
23- contains the * UserController* class (we'll come to that later), you'll need many view
18+ for example one file for your controller in the * actions* directory you'll need many view
2419files. Thus, instead of saving your views directly in the * views* directory you will have to save
25- them in a folder named after your action .
20+ them in a folder named after your controller .
2621
2722When using the router, the * action* parameter is mandatory. This plugin adds another mandatory
2823parameter named * controller* . This parameter refers to the controller name whereas the * action*
@@ -44,19 +39,23 @@ The default controller name is *index* and the default action name is *index*.
4439
4540### Creating simple controllers
4641
47- As said before, a controller is a class. The only condition is in the naming convention. Your class has to be named
48- using the controller name starting by an upper case letter suffixed with * Controller* . So for example,
49- with a controller named * user* (saved in * app/actions/user.php* ), the class name will be * UserController* .
42+ As said before, a controller is a class. It must inherits from ` Atomik\Controller\Controller ` and respect
43+ a naming convention. Your class has to be named using the controller's name starting by an upper case letter
44+ suffixed with * Controller* .
45+
46+ Controller classes will be loaded, as any other classes, with the autoloader. Thus, your files must be
47+ named after your controller class.
5048
51- If the action file is located in a sub folder, the class name as to follow the PSR-0 convention.
52- For example, if the file is * app/actions/auth/user.php* the class name will be * Auth\UserController* .
49+ So for example, with a controller named * users* , it must be saved in * app/actions/UsersController.php* and
50+ the class name will be * UsersController* .
51+
52+ If the action file is located in a sub folder, the class name has to follow the PSR-0 convention.
53+ For example, if the file is * app/actions/Auth/UsersController.php* the class name will be ` Auth\UsersController ` .
5354
5455Then add public methods to your class. All public methods which does not start with an underscore will
5556be callable as an action.
5657
57- Don't forget to also create the view associated to each action.
58-
59- class UserController
58+ class UsersController extends Atomik\Controller\Controller
6059 {
6160 public function index()
6261 {
@@ -67,38 +66,39 @@ Don't forget to also create the view associated to each action.
6766 }
6867 }
6968
70- Also create two view files: * app/views/user/index.phtml* and * app/views/user/login.phtml* .
71- Note that they are saved under the * app/views/user* directory where the last folder is the
72- controller name.
69+ The associated views must be located in the * app/views/users* .
70+ In our example, it would be * app/views/user/index.phtml* and * app/views/user/login.phtml* .
7371
7472You can then use the following urls: < http://example.com/user > or < http://example.com/user/login > .
7573
76- In classic actions, all defined variables where accessible from the view. This is not possible
74+ In classic actions, all defined variables were accessible from the view. This is not possible
7775anymore when using methods for scoping reasons. To forward variables to the view, simply define
78- class properties.
76+ class properties or return an array from your action method .
7977
80- // app/actions/user .php
78+ In * app/actions/UsersController .php* :
8179
82- class UserController
80+ class UsersController extends Atomik\Controller\Controller
8381 {
82+ public $title = 'Users';
83+
8484 public function index()
8585 {
86- $this-> username = 'peter';
86+ return array(' username' => 'peter') ;
8787 }
8888 }
8989
90- // app/views/user/index.phtml
90+ In * app/views/user/index.phtml* :
9191
92+ <h1><?php echo $title ?></h1>
9293 hello <php echo $username ?>
9394
94- ### Creating controllers by subclassing Atomik\Controller\Controller
95-
96- Subclassing Atomik\Controller\Controller when creating a controller class brings some nice features.
95+ ### Controller utilities
9796
98- First of all, you can define two methods ` _before() ` and ̀_ after()`
99- that will be called before and after each action.
97+ First of all, you can define two methods ` preDispatch() ` and ` postDispatch() `
98+ that will be called before and after each action. You can also define an ` init() `
99+ method which will be called after the constructor.
100100
101- Secondly, route parameters will be automatically mapped to method arguments.
101+ Route parameters will be automatically mapped to method arguments.
102102
103103 Atomik::set('app.routes', array(
104104 'archives/:year/:month' => array(
@@ -119,3 +119,10 @@ Secondly, route parameters will be automatically mapped to method arguments.
119119
120120The ` $year ` and ` $month ` argument will be taken from the route parameters.
121121The order is not important.
122+
123+ ## Using controllers in Pluggable Apps
124+
125+ The plugin will be disabled when a pluggable application starts. It can be re-enabled using
126+
127+ $config = array();
128+ Atomik\Controller\Plugin::start($config);
0 commit comments