Skip to content
This repository was archived by the owner on May 22, 2024. It is now read-only.

Commit 94d23ab

Browse files
committed
fixed documentation for Controller plugin
added possibility to return view vars as an array from controller methods
1 parent 3306807 commit 94d23ab

3 files changed

Lines changed: 62 additions & 33 deletions

File tree

docs/controllers.md

Lines changed: 37 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,14 @@ actions.
1010
This plugin adds support for controllers to Atomik. Once activated you must use controllers in your
1111
actions. 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

1915
There are two major differences which are views and the router.
2016

2117
Each 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
2419
files. 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

2722
When using the router, the *action* parameter is mandatory. This plugin adds another mandatory
2823
parameter 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

5455
Then add public methods to your class. All public methods which does not start with an underscore will
5556
be 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

7472
You 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
7775
anymore 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
120120
The `$year` and `$month` argument will be taken from the route parameters.
121121
The 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);

src/plugins/Controller/Controller.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,4 +168,22 @@ protected function _setHeader($name, $value)
168168
{
169169
header("$name: $value");
170170
}
171+
172+
protected function _get($key, $default = null)
173+
{
174+
return Atomik::get($key, $default);
175+
}
176+
177+
protected function _flash($message, $label = 'default')
178+
{
179+
if (!Atomik::isPluginLoaded('Flash')) {
180+
throw new AtomikException("Controller::_flash() needs the 'Flash' plugin");
181+
}
182+
return Atomik::flash($message, $label);
183+
}
184+
185+
protected function _redirect($url, $useUrl = true, $code = 302)
186+
{
187+
return Atomik::redirect($url, $useUrl, $code);
188+
}
171189
}

src/plugins/Controller/Plugin.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public static function start(&$config)
3838
'default_action' => 'index',
3939

4040
// directories where to find controllers
41-
'dirs' => 'app/controllers',
41+
'dirs' => array('app/actions', 'app/controllers'),
4242

4343
// default controller namespaces
4444
'namespace' => ''
@@ -80,10 +80,14 @@ public static function execute($action, $method, $vars, &$context)
8080
}
8181

8282
$instance = new $className();
83-
if (($instance->_dispatch($action, $method, $vars)) === false) {
83+
if (($vars = $instance->_dispatch($action, $method, $vars)) === false) {
8484
return false;
8585
}
86-
return get_object_vars($instance);
86+
87+
if (!is_array($vars)) {
88+
$vars = array();
89+
}
90+
return array_merge(get_object_vars($instance), $vars);
8791
}
8892
}
8993

0 commit comments

Comments
 (0)