I suppose that if you’re here you already know what is an MVC pattern and what a controller is expected to be. If this is not the case and only for advice, i only can tell you that you’re certainly not at the right place to learn it. So make a google search and come back after as we like to say in my world, “knowledge has value only if shared by all” but please “read the fucking manual”.
In simpleMVC you will perhaps be surprise that you can’t find a “real” frontController class. The choice i made was to use what other called bootStrap as a frontController or sometimes also named dispatcher. This is a sort of all in one. To have a better understanding of the bootStrap see bootStrap settings
Action controller is the place to process the client request and prepare datas for the view.
If you use abstracModels a big part of the datas manipulations will be done in the models.
Most of the time actions controllers will just extends abstractController and define actions methods.
so let’s create our first actionController.
#!php
<?php
class defaultController extends abstractController{
function helloAction(){
$this->view->assign('hello'=>"this is a hello world example");
}
}
this really simple controller will handle request to http://oursite.com/sandBox/default/hello if we use rewrite rules or to http://oursite.com/sandBox/?ctrl=default&action=hello.
the only job it does for now is to prepare assign a variable ‘hello’ for the view, and then render the view.
Extending abstractControllers give you some default behaviour to you actionController but it also give you some space to tweak it to sweet your needs.
The init method is a sort of extended constructor, in fact this method is always called at the end of the constructor. It is also the place where controllers get linked to a viewInterface object so if you want to redefine or extend this method it’s best to call parent::init() at the begining.
As an example let’s imagine we have a controller statisticsController and we want to be sure that a session variable ‘canSeeStats’ is set to allow user to do any action depending on that controller. One way of doing this can be to perform the check in the init method of our statisticsController.
#!php
<?php
class statisticsController extends abstractController{
function init(){
// start by default init before to do our customs stuffs
parent::init();
// now do our check
if( empty($_SESSION['canSeeStats']) ){
self::appendAppMsg('You can\'t access this page','error');
return $this->redirectAction('pages:error'); // this return true, make a redirection and stop script execution
}
}
}
this example introduce 2 controllers methods, appendAppMsg and redirectAction.
You can define pre*Action and post*Action methods inside your controllers. They will be automaticly called before and after each action.
For example consider the defaultAction method, here’s the stack of call the controller will made:
So considering our previous example statisticsController if we want to check for a session var only for the detailedAction method we can do it in a preDetailedAction method instead of the init:
#!php
<?php
class statisticsController extends abstractController{
function preDetailedAction(){
// preform our check
if( empty($_SESSION['canSeeStats']) ){
self::appendAppMsg('You can\'t access this page','error');
return $this->redirectAction('pages:error'); // this return true, make a redirection and stop script execution
}
}
}