Before everyone groans 'not another missing model component question' I
completely understand why a Model component is not included in the
framework.  After all a model is a domain-specific representation of part of
an application so there is little to be gained from creating some sort of
generalization of a 'model'. Rather than be included in the framework, model
classes should be created as required by a particular application.

However I would like some advice as to how a true MVC application should be
designed using the framework.  Given the current jumble of
application/control/view logic of my current system I want to be absolutely
sure I do thing properly from the outset when designing new systems using
the ZF.

I have been closely following the 'Understanding the Zend Framework'
tutorials on IBMs developerWorks website and it has been an excellent
resource, hopefully others will be familar with it.
http://www-128.ibm.com/developerworks/views/opensource/libraryview.jsp?search_by=understanding+the+zend+framework

The tutorial chronicles the creation of an online RSS/Atom feed reader.  The
first few tutorials showed how easy it was to organise systems into
controller->action requests and how the Zend_Filter_Input and Zend_Db
components worked.  But all the code was included within the controller.

My question is this.

In MVC, if the controllers purpose is to interpret input from the view and
manipulate the model based on that input (decoupling the view from the
model) then shouldn't all the input validation code and database code be
wrapped up in a model and never be included in the controller?  And a
controller should be increadibly simple and just call methods in a model
based on a particular action.

e.g. The following is my take on  how a controllers logic should be if
performing a simple insert into a database based on input from a form
(certain values passsed to the view are not listed for brevity)

class MessageController extends Zend_Controller_Action
{
    public function insertAction()
   {
       $messageModel = new MessageModel;
        // If there was a problem with the input then pass details of the
error back to the form
        if(!$messageModel->validateInsert())
        {
            $view = Zend::registry('view');
            $view->field_error_array = $messageModel->getLastErrors()
            echo $view->render('addMessage.php');
        }
       // Otherwise just call the insert() method and go back to a list of
existing messages
        else
        {
            $messageModel->insert();
            $view = Zend::registry('view');
            echo $view->render('viewMessages.php');
         }
    }

    ... other actions
}

Although I have found the IBM tutorial incredibly useful... I just want to
make sure I wasn't developing bad habits early. I can see that whacking all
the code in the controller is quick and easy and would probably work fine
for small applications but a large application would suffer wouldn't it?

Can someone clarify how strict MVC should work with the Framework, is my
take on controller logic correct?. Does anyone know of any tutorials which
follow strict MVC?
-- 
View this message in context: 
http://www.nabble.com/Advice-for-Model-design-tf2311120.html#a6425632
Sent from the Zend Framework mailing list archive at Nabble.com.

Reply via email to