DooPHP Controller
Controller is a class file that associated with an URI route which is located in SITE_PATH/protected/controller.
Controller class in DooPHP extends the DooController class. Thus, there are some names that cannot be used in your class properties & methods. The following is a list of reserved names:
//properties $params $puts $extension $autoroute //Methods init_put_vars() load() language() accept_type() render() renderc() setContentType() is_SSL() view() db() cache() acl() beforeRun()
Basic Example:
A URI route is defined with the Controller name and its action method. For example:
$route['*']['/blog/all'] = array('BlogController', 'showAll');
When a user visits the URL http://localhost/index.php/blog/all the BlogController will be created and the showAll() method of the class is executed.
class BlogController extends DooController {
function showAll() {
echo 'Hello World!';
}
}
The result of the above example would be: Hello World!
A controller class must be named with Controller at the end, eg. NewsController and the file name should be the same as the class name, NewsController.php
Parameters in Controller
In a web application, you might want to utilize the use of parameter passed in via URL.
$route['*']['/archive/:year/:month'] = array('NewsController', 'archive');
Users can passed in values to your app, eg. http://domain/archive/2009/12
In the NewsController you can get the values by calling $this->params:
class NewsController extends DooController{
function archive(){
echo $this->params['year'];
echo $this->params['month'];
}
}
If you are accessing with auto routing on, you need to use indexed digits because you do not need to define any routes. Example: http://domain/news/archive/2009/09
class NewsController extends DooController{
function archive(){
echo $this->params[0];
echo $this->params[1];
// unlimited ...
}
}
Extension names
In the route definition, one can defined a route access with a/many extension names. You can retrieve the extension names the visitor is accessing via $extension. Extensions are retrieved along with a dot '.' (.html, .json, .xml , ...)
class TestController extends DooController{
function testing() {
echo $this->extension;
}
}
Controllers in Subfolder
You may group your controller files in a subfolder inside protected/controller. For example, you might have an application with admin features and would want to put controller files related to the admin in a subfolder called admin
Folder structure:
protected/
controller/
MainController.php
PhotoController.php
admin/
AdminController.php
AdminPhotoController.php
With the structure above, you need to define your routes with the subfolder name:
$route['*']['/admin/photo'] = array('admin/AdminPhotoController', 'main');
Rerouting from a Controller
There are times you need to 'redirect' users to another method that handles some other logics. For example, an admin site requires login. If a user access a route (which does some admin stuffs), we need to check that if he is logined. If he's not, we might need to display an error.
class AdminController extends DooController{
function editPhoto() {
if( User is not login)
return array('/error/notlogin', 'internal');
}
}
The system will reroute to another route (/error/notlogin) which is defined in routes.conf.php. This is in the same user request not a redirection, meaning no page refreshed or address URL changed.
The above example is an internal rerouting, however, you can make page redirection by just returning some values in a method.
Try return these values:
404 #send 404 header
array('/internal/route', 404) #send 404 header & internal reroute
'http://www.google.com' #redirect to URL. 302 Found sent
array('http://www.google.com', 301) #redirect. 301 Moved Permenantly
array('/hello/sayhi', 'internal') #reroute internally, 200 OK
See the power of DooController? There's no need for a complex & slow hooks or internal system hacks. Let's go extending it to be more useful.
Extending DooController
The reroute example above is not really complete. In real life, we might want to handle authentication logic in one place which is much more maintainable. To do so, we can have a controller call AuthController which handles user authentication.
The AuthController would have to extend DooController.
class AuthController extends DooController{
function auth(){
if(session check, not login? no permission?)
return array('/error/login', 'internal');
else
//do something, but return nothing
}
}
Then we will have the AdminController to extend AuthController and call the auth method to check if the user is logined.
// need to import the controller class first.
Doo::loadController('AuthController');
class AdminController extends AuthController{
function adminstuff(){
if($rs = $this->auth()) return $rs;
//do stuff here since we are login
echo 'Hi there! You are login. Please administer.';
}
}
By extending the controller, we can have authentication handling in one file. If we need to change how unauthorized access is handled, we can simply edit the auth() method in AuthController. We don't have to modify every method which requires authentication.
