douou8954 2012-02-29 21:06
浏览 299
已采纳

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

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
        }
    }
  • 写回答

4条回答 默认 最新

  • doudianhuo1129 2012-02-29 21:10
    关注

    Just have your register function in your model 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.

    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];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?