cgm225 Posted April 13, 2008 Share Posted April 13, 2008 I am still very new to PHP, and am creating a simple MVC front controller. Everything works, but I have an ActionController class that basically contains my MVC "View" functionality within the displayView() function. I want to split the following class into (1) an ActionController class and (2) View class. How would you split this class into those? What subclasses would you include where? I guess this is an opinion/best practice question. I just want to make sure I am keeping my business and presentation logic seperated. Thanks for your help in advance! <?php abstract class ActionController { //Declaring variables protected $name; protected $viewData = array(); protected $content; private static $pageDir; //Setting the page directory, which is passed from the FrontController public static function setPageDir($pageDir){ self::$pageDir = $pageDir; } public function setName($name) { $this->name = $name; } public function getName() { return $this->name; } //Placing a value in the $viewData array at the key postion public function setVar($key, $value) { $this->viewData[$key] = $value; } //Returns a value from the $viewData array located at the key position public function getVar($key) { if (array_key_exists($key, $this->viewData)) { return $this->viewData[$key]; } } /* Checking for actionMethod in the Actions class (example: doFrontpage() within home/HomeActions.php5) with the method_exists function and, if present, the actionMethod and displayView functions are executed. */ public function dispatchAction($action) { $actionMethod = "do" . ucfirst($action); if (!method_exists($this, $actionMethod)) { throw new FrontControllerException("Action Method not found!"); } $this->$actionMethod(); $this->displayView($action); } public function displayView($action) { if (!is_file(self::$pageDir . "/" . $this->getName() . "/" . $action . "View.php5")) { throw new FrontControllerException("Action View not found!"); } //This foreach function goes over all of the elements in $this->viewData array, creates //a variable for every value, and the name of the value (the variable name) is the key's value. foreach ($this->viewData as $key => $value) { $$key = $value; } $this->content = PAGE_DIR . "/" . $this->getName() . "/" . $action . "View.php5"; include_once PAGE_DIR . "/template.php5"; } public function __set($key, $value) { $this->setVar($key, $value); } public function __get($key) { return $this->getVar($key); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/100932-my-action-controller-and-view-classes-need-to-be-seperated-design-question/ Share on other sites More sharing options...
cgm225 Posted April 13, 2008 Author Share Posted April 13, 2008 bump Quote Link to comment https://forums.phpfreaks.com/topic/100932-my-action-controller-and-view-classes-need-to-be-seperated-design-question/#findComment-516327 Share on other sites More sharing options...
Daniel0 Posted April 13, 2008 Share Posted April 13, 2008 Well, you could remove all methods that deal with views and move them into another class. You could then an instance of a view in the ActionController constructor as a class property. Quote Link to comment https://forums.phpfreaks.com/topic/100932-my-action-controller-and-view-classes-need-to-be-seperated-design-question/#findComment-516335 Share on other sites More sharing options...
cgm225 Posted April 14, 2008 Author Share Posted April 14, 2008 Would you show me how you would split it? Thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/100932-my-action-controller-and-view-classes-need-to-be-seperated-design-question/#findComment-516870 Share on other sites More sharing options...
Daniel0 Posted April 14, 2008 Share Posted April 14, 2008 Perhaps like this: <?php class View { protected $data = array(); // old: ActionController:$viewData public function get($key); // old: ActionController::getVar(); public function __get($key); public function set($key, $value); // old: ActionController::setVar(); public function __set($key, $value); public function display($action); // old: ActionController::view(); } class ActionController { protected $name; protected $content; /** * @var View */ protected $view; private static $pageDir; public function __construct() { // somehow set the view: $this->view = $theView; } // etc. } ?> I didn't bother to write the method bodies. Quote Link to comment https://forums.phpfreaks.com/topic/100932-my-action-controller-and-view-classes-need-to-be-seperated-design-question/#findComment-516973 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.