Zend framework: what is the right place to check user input?

I want to add a user to the user table via a link, for example '/ index / adduser / id / 7'.

Question

Should I check user input inside the adduserAction function inside the controller or somewhere inside the model file? I put files containing database related functions inside the "models" directory. Suppose the user is added to the table via "id". This identifier is sent via 'get'. And finally, it was added to the table function "AddUser" (inside the model file). Then I have to check this "id" inside the "adduserAction" or "AddUser".? Scalability, would it be better to do this inside AddUser?

+3
source share
5 answers

I would say put validation in your model. You can then save your validation rules in a central location. How should your controller know the exact length of a valid username? This is a model territory. Your controller may ask the model if the username length is true or false, but the rule itself should be in your model. In my controller, I would do something like this:

$ model = new model; $ model-> loadFromArray (something to get the message); if (! $ model-> isValid ()) {forward back to the form} $ Models-> Save ();

+7
source

There is a popular belief / paradigm that reads:

Thin controllers, fat models.

, , , , . , . ... . 1 .

, .NET ( - ) ( ):

Controller
  -> ServiceLayer
     -> Repository
        -> DataObject

, . , , Zend Framework.

. . :

  • ServiceLayer:
    -. : Dataobjects (Models) , , , ..
  • Repository:
    (, ), DataObjects DataObjects ( )
  • DataObject:
    . , DataObjects ( ), . , XML -, DataObject - , , .

, . MVC . - , - , .

, , ​​ . , , 1 . - . ( ).

+8
+2

, . , , . .., .

0

, , , " " - , , . :

  • .

  • .

:

//UserModel
class Default_Model_User
{

    protected $_form;

    public function getForm()
    {
        if(!isset($this->_form)) {
            $this->_form = new Default_Model_Form_User();
        }
    }

    public function validate($data)
    {
        if($result = $this->getForm()->isValid($data)) {
        // if you have custom validation conditions outside of the form, then you 
        // can do the validation here also.
            if($data['controller_entered'] == 'some value') {
                $result = false;
            }
        }
    return $result;
    }

    public function saveUser($data)
    {
        if($result = $this->validate($data)) {
            // do something.
        }
    return $result;
    }
}

, "", URL-:

http://www.survivethedeepend.com/zendframeworkbook/en/1.0

: http://www.survivethedeepend.com/zendframeworkbook/en/1.0/implementing.the.domain.model.entries.and.authors#id1491270

You will undoubtedly encounter other problems as you move ... This can help check the comments on my blog post regarding the model layer where I touch on this issue: http://www.rvdavid.net/my-zend-framework- model-layer-part-service-part-orm /

Hope this helps someone.

0
source

Source: https://habr.com/ru/post/1730443/


All Articles