Source for file ActionController.class.php
Documentation is available at ActionController.class.php
* MVC framwork magic happens here!
* @copyright 2009-2010 NADEO
* This is the base class for all controllers. Extend ActionController to create
* a new controller for your application.
* <b>Naming conventions</b>
* <li>Controller classes are suffixed by "Controller", naming is regular CamelCase class convention</li>
* <li>Actions are regular camelCase convention</li>
* <li>When creating links with the Request engine, use class and method names eg. createLink('SomeController', 'someAction')</li>
* <li>Views folders and files use the same naming conventions as the classes/methods (eg. view/SomeController/someAction.php)</li>
* <li>The URLs will be lowercase (camelCase is mapped to underscore-separated names)</li>
* <li>You can change the default separator ("_") in the config using the APP_MVC_URL_SEPARATOR constant</li>
* class HomeController extends ActionController
* $this->addFilter(new RegisterRequestParametersFilter());
* function index() {} // mapped by /home/index/
* function anotherAction() {} // mapped by /home/another_action/
* Overrride this to define the controller's default action name
* Current controller name
protected $filters =
array();
* @var array[ReflectionMethod]
protected $reflectionMethods =
array();
final public static function dispatch()
self::getController($request->getController())->launch();
* @return ActionController
final static public function getController($controllerName)
$controllerClass =
$controllerName.
'Controller';
throw
new ControllerNotFoundException($controllerName);
require_once($controllerFilename);
return new $controllerClass($controllerName);
* If you want to do stuff at instanciation, override self::onConstruct()
function __construct($controllerName)
* Stuff to be executed when the controller is instanciated; override this in your controllers
* Add a filter to the curent controller
* Typically you should call that in your controller's onConstruct() method
* class SomeStuffController extends ActionController
* $this->addFilter(new UserAgentCheckFilter());
final protected function addFilter(Filterable $filter)
$this->filters[] =
$filter;
* @return array[Filterable]
final public function getFilters()
* Executes an action from within another action
final protected function chainAction($controllerName, $actionName)
$this->checkActionExists($actionName);
$this->executeAction($actionName);
$this->executeActionCrossController($controllerName,$actionName);
* Executes an action from within another action and override the view from the first action
final protected function chainActionAndView($controllerName, $actionName, $resetViews =
true)
$this->checkActionExists($actionName);
$this->executeAction($actionName);
$this->executeActionCrossController($controllerName, $actionName);
final public function checkActionExists($actionName)
$this->reflectionMethods[$actionName] =
new ReflectionMethod(get_class($this),$actionName);
throw
new ActionNotFoundException($actionName);
if(!$this->reflectionMethods[$actionName]->isPublic())
throw
new ActionNotFoundException($actionName.
' (Method "'.
$actionName.
'()" must be public)');
if($this->reflectionMethods[$actionName]->isFinal())
throw
new Exception($actionName.
' (Method "'.
$actionName.
'()" must not be final)');
final protected function executeActionCrossController($controllerName, $actionName)
$controller =
self::getController($controllerName);
$controller->checkActionExists($actionName);
$controllerFilters =
$controller->getFilters();
foreach($controllerFilters as $controllerFilter)
if(!in_array($controllerFilter,$this->filters))
$controllerFilter->preFilter();
$controller->executeAction($actionName);
foreach($controllerFilters as $controllerFilter)
if(!in_array($controllerFilter,$this->filters))
$controllerFilter->postFilter();
final public function executeAction($actionName)
$this->reflectionMethods[$actionName] =
new ReflectionMethod(get_class($this),$actionName);
throw
new ActionNotFoundException($actionName);
$callParameters =
array();
$requiredParameters =
$this->reflectionMethods[$actionName]->getParameters();
foreach($requiredParameters as $parameter)
if($parameter->isDefaultValueAvailable())
$callParameters[] =
$this->request->get($parameter->getName(), $parameter->getDefaultValue());
$pname =
$parameter->getName();
$pmessage =
'Undefined parameter: $<$o'.
$pname.
'$>';
$callParameters[] =
$this->request->getStrict($pname, $pmessage);
final protected function launch()
$this->checkActionExists($actionName);
foreach($this->filters as $filter)
$this->executeAction($actionName);
class ControllerNotFoundException extends MVCException {}
class ActionNotFoundException extends MVCException {}