开发者

Help getting Model into my Controllers, with MVC

I have been working on my own library/framework for the learning experience for a while. MVC is one of those things that took me a while to really understand but I do finally "Get it".

Below is some sample code for a basic MVC setup in PHP. I think I am in the right direction so far, where I need a little help is down in the "Example controller" near the bottom, you will see where I can create a view, I just need to figure out how to best get my data from a model file into that controller class. Please help with example code if you can, hopefully I am making sense.

Also I am welcome to any comments/suggestions on any of the code

Abstract Controller class...

/**
 * MVC Example Project
 */


/**
* Extend this class with your Controllers
* Reference to the model wrapper / loader functions via $this->model
* Reference to the view functions via $this->view
*/
abstract class Core_Controller {
    protected $view;
    protected $model;

    function __construct($dependencyContainer){
        $this->view = new Core_View();
        //$this->view = $dependencyContainer->get(view);


    }

}

Abstract Model class...

/**
* Extend this class with your models and reference to the database object via $this->$db
*/
abstract class Core_Model {
    protected $db;
    protected $session;

    function __construct($dependencyContainer) {
        $this->db = $dependencyContainer->get(database);
        $this->session = $dependencyContainer->get(session);
    }

}

View class, might make it abstract as well...

class Core_View {

    protected $data;

    # Load a view file (views/$view.php);
    # $param data this gets extracted and be thus be used inside the view
    # When loading another view from inside the view file the data is 'cached' so you
    # don't have to pass them again
    public function load($view,$data = null) {

        if($data) {
            $this->data = $data;
            extract($data);
 开发者_如何学Python       } elseif($this->data != null) {
                extract($this->data);
        }

        require(APP_PATH . "Views/$view.php");
    }

    public function set($data = null) {

        if($data) {
            $this->data = $data;
            extract($data);
        } elseif($this->data != null) {
                extract($this->data);
        }

    }
}

Example putting it together...

/**
 * Example Controller
 */
class User_Controller extends Core_Controller {

    public function profile()
    {
        $profileData = array();
        $profileData = //GET from Model

        $this->view->load('userProfile', $profileData);
    }
}
?>


My suggestion is not to tie view and model to the controller at all. Let them be instantiable from controller code, just like any other classes. You can then get the model data (and pass it to the view) in standard object oriented way.


Will you use a Data access layer (DAL) / Object-relational mapping (ORM)? Take a look at Zend_Db, Doctrine or Propel

I'd say that you're missing the part of the application that manipulate your models. It could be your controller, but isn't a good practice. So we need a model mapper.

The best way to get model data from your controller is simply calling it. But generally we use a kind of "pointer" which knows how to populate your object model. This pointer is called "Mappers" (Data Mapper Pattern):

$MyModelMapper = new MyModelMapper();
$Profile = $MyModelMapper->getProfileById($id); // return Core_Model.

This function will perform a database query and will populate one specific model with the data. You could also get an array of objects for a "list" action for example.

Then you'll pass this model to your view.

I think you should take a look at the Zend Framewok quick start. It will give you some ideas.

See this question too: What's the difference between DAO and Data Mapper

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜