简体   繁体   English

如何将数据从模型传递到视图(或控制器)

[英]How to pass data from model to view(or to controller)

The question is simple: How can i pass data from model to view(or back to the controller) to display errors like "your password is too short" 问题很简单:如何将数据从模型传递到视图(或传回控制器)以显示诸如“您的密码太短”的错误

Here is the controller 这是控制器

class UsersController extends Controller {

    private $username;
    private $password;

    function register()
    {
        if($_POST)
        {
            $this->User->username = $_POST['username'];
            $this->User->password = $_POST['password'];
            $this->User->register();
        }
    }



}

the model 该模型

class User extends Model {

    public $username;
    public $password;

    function register()
    {
        $username = $this->username;
        $password = $this->password;

        if (!empty($username) && !empty($password))
        {
            // registration process
        }
        else
        {
            // "you must provide a username and password" or something like that
        }
    }

Just have your register function in your model return "PASSWORD"; 只需在模型中使用您的register功能即可return "PASSWORD"; to the controller and have your controller take the return from the model and return it to the view. 到控制器,让您的控制器从模型中获取收益并将其返回到视图。 Let the view interpret what the error output for "PASSWORD" is. 让视图解释“ PASSWORD”的错误输出是什么。

Example: 例:

the controller 控制器

class UsersController extends Controller {

    private $username;
    private $password;

    function register()
    {
        if($_POST)
        {
            $this->User->username = $_POST['username'];
            $this->User->password = $_POST['password'];
            return $this->User->register();
        }
    }
}

the model 该模型

class User extends Model {

    public $username;
    public $password;

    function register()
    {
        $username = $this->username;
        $password = $this->password;

        if (!empty($username) && !empty($password))
        {
            // ...
            return "SUCCESS";
        }
        else
        {
            return "PASSWORD";
        }
    }
}

the view 风景

$responses = array("SUCCESS" => "Registered Successfully!", "PASSWORD" => "You must provide a username and password!");

$result = $this->UsersController->register();
echo $responses[$result];

Simply have your model methods to return a value, or throw exceptions, like any normal method. 像任何普通方法一样,只需让您的模型方法返回值或引发异常即可。 Then handle it in the controller. 然后在控制器中处理它。 The view shouldn't touch the data directly from the model, that's the controller's job. 视图不应直接接触模型中的数据,这是控制器的工作。

public function addAction()
{
    $form = $this->_getForm();
    $this->view->form = $form;
    $this->render('add', null, true);
}

public function editAction()
{
    $id = $this->getRequest()->getParam(0);
    $Model = DI::get('yourclass_Model');
    $form = $this->_getForm();
    $data = $Model->getData();
    $form->populate($data);
    $this->view->flashMessages = $this->_helper->FlashMessenger->getMessages();
    $this->view->form = $form;
    $this->render('add', null, true);
}

public function saveAction()
{
    $form = $this->_getForm();
    $Model = DI::get('yourclass_Model');
    try{
    $saved = $Model->saveForm($form, $_POST);
    } catch (Exception $e) {
       echo "<pre>";
       print_r($e);
       exit;
    }
    if($saved)
    {
        $this->_helper->FlashMessenger('Record Saved!');
        $this->_redirect("edit".$form->id->getValue(), array('exit'=>true));
    } 
    $this->view->errorMessage = 'There were some errors';
    $this->view->form = $form;
    $this->render('add', null, true);
}

Create a class which implements Singleton pattern and ArrayAccess interface. 创建一个实现Singleton模式和ArrayAccess接口的类。 Or create something similar with dependency injection. 或者用依赖注入创建类似的东西。

The ultimate solution would be if you create some validation architecture. 最终的解决方案是创建一些验证体系结构。 (The model validates its self and it's error state is available in the views.) (模型验证其自身,并且错误状态在视图中可用。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM