I have too played with MVC for a while, and I came up with this kind of idea.
PHP Code:
<?php
abstract class JMVC_ActionController{
public $actions=array(); //Array of registered actions
protected $model; //Reference to model
protected $view; //Reference to View
protected $requestPath;
public function __construct($requestPath){
$this->requestPath = $requestPath;
$this->initialize(); //Initialize before executing actions
foreach($this->actions as $action){
if (isset($_REQUEST[$action->getName()])) { //Execute requested actions
$action->execute();
$action->isExecuted() && $this->afterAction($action);
}
}
$this->main();
}
/**
* JMVC_ActionController::initialize()
*
* Initialize variables and register actions here.
*
* @return
**/
abstract protected function initialize();
/**
* JMVC_ActionController::afterAction()
*
* Method is called each time when any of the registered actions is executed.
*
* @return
**/
protected function afterAction(Action $action){}
/**
* JMVC_ActionController::main()
*
* Method is called after all requested actions are executed.
*
* @return
**/
abstract protected function main();
/**
* JMVC_ActionController::registerAction()
*
*
* @return
**/
protected function registerAction($name,$methods){
$methods = is_array($methods) ? $methods : array($methods);
$this->actions[$name]= new Action($name,$this,$methods);
}
}
class Action{
public $value = '';
public $name = '';
private $sender;
private $methods = array();
private $executed = false;
public $error = '';
public function __construct($name,JMVC_ActionController $sender,$methods){
if (!method_exists($sender, 'action_'.$name)) {
Throw new Exception('Action method ('.$name.') not implemented at Class: '.get_class($sender));
}
$this->name = $name;
$this->methods = $methods;
$this->sender = $sender;
}
public function getName(){
return $this->name;
}
public function getValue(){
return $this->value;
}
public function isExecuted(){
return $this->executed;
}
public function execute(){
$value = false;
if (in_array('POST', $this->methods) && isset($_POST[$this->name])) {
$value = empty_val($_POST[$this->name.'_value']);
}elseif (in_array('GET', $this->methods) && empty($value) && isset($_GET[$this->name])) {
$value = empty_val($_GET[$this->name]);
}else{
return;
}
try{
$this->value = $this->sender->{'action_'.$this->name}($value);
}catch (Exception $e){
if ($e instanceof ActionException) {
$this->error = $e->getMessage();
}
}
$this->executed = true;
}
}
?>
And here's a very simple example.
PHP Code:
class Controller_example extends JMVC_ActionController{
function initialize(){
$this->registerAction('add','GET');
$this->registerAction('subtract','GET');
}
function main(){
$this->view = new View_example($this->model);
$this->view->display($this->requestPath);
}
function action_add($value=NULL){
$this->model->value++;
}
function action_subtract($value=NULL){
$this->model->value--;
}
}
}
Idea is that you have to make own methods to every action in the controller class. Now you can call the script like this: index.php?add (action_add method is called)
index.php?subtract or (action_subtract is called)
index.php?add&subtract (both methods are called)
Basicly you can use extended ActionController class as front controller or page controller. So I'd just like to know what more expereinced OOP-gurus think about this. And please don't mind if my english is not very fluent, i type it so rarely. But I can read it very well
Bookmarks