开发者

What are the most common things that get put into controllers when they should really be in models, and vice versa?

开发者 https://www.devze.com 2023-02-24 05:03 出处:网络
I\'m still trying to wrap my mind around the MVC pattern and exactly what is supposed to be placed in Controllers, and what should be placed in Models.I\'ve read that controllers mostly contain applic

I'm still trying to wrap my mind around the MVC pattern and exactly what is supposed to be placed in Controllers, and what should be placed in Models. I've read that controllers mostly contain application logic, and models should have all the business logic. It's sometimes hard to differentiate between the two. Where exactly开发者_如何学JAVA do you draw the line? What types of actions are on the fringe of both?


MVC is a rather loaded term, it means different things to different authors.

When it was introduced in Smalltalk, controllers were for user input, views were for output, and models were for state related to the problem space.

Apple also uses the term MVC in its documentation for iOS and Mac OS, but there the model is for basically database access/problem space, the view is for both input and output and controllers are for wiring Models and Views together.

MVC is used by others as well, and again the terms mean slightly different things in each case, with the controller being the most variable between them. All this means that answering your question is quite difficult.

In the abstract though, the model should consist of that which a domain expert would understand. The line drawing you talk about has mostly to do with validation of user input which is often put in the controller because the response to invalid input is usually application dependent and often bad input needs to be weeded out before sent off to the model in a distributed system.

Hope this helps somewhat, but the question itself is vague, so the answer will be too.


Data access (e.g. Database calls) should be in the Model.

User input should be handled by the Controller (where it may be handed off to a Model).


This is good post on model-controller thing

as for ZF mvc implementation i think best approach is to keep almost everything in you model rather than controller. Controller is just for mapping user input to appropriate model methods.
My current model have several layers: static service locator, services(kinda business logic layer), models, and mappers.

Though my current implementation is bad in terms of OOP so i won't give you more details.

And quick example action from my app, simple catalog module:

public function addAction()
{
    $service = Xrks_Service::getInstance()->getService('catalog', true);
    if(!$service->checkAcl('addItem')) {
       throw new Exception('You have no rights to view this page', 403);
    }
    $item = $service->getNewItem();
    $form = $service->getEditForm($item); //here form created and filled with default values from model
    if ($this->getRequest()->isPost() && $form->isValid($this->getRequest()->getPost())) {
        $service->populateModel($item, $form); //form->model mapper hidden behind service interface
        $item->save();
        $this->_helper->getHelper('Redirector')->gotoRouteAndExit(array('action' => 'edit', 'id' => $item->getId()), 'catalog-item');
    }
    $this->view->form = $form;

}
0

精彩评论

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