Jump to content

My Action controller and View classes need to be seperated +/- Design question..


Recommended Posts

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);
    }
}

?>

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.