doubi4531 2013-03-24 10:13
浏览 13
已采纳

将用户友好的消息返回到视图

This is a summary of how a user would login in my application(up to validating the data):

  1. Submit the login form.

  2. Router figures out where the user is trying to go.

  3. The correct Controller is initialized.

  4. The RecognitionService is started and the login() method is run.

  5. Within the login() method I set the supplied username and password in my User domain object.

  6. My User domain object will check if the username and password are valid by checking their type, length, format etc.

All this is good except for when the entered username contains invalid characters and then getting the error message from my domain object back to the View in a neat and tidy way.

At the moment I am having my domain objects return the message (well really the array key to the message) to the RecognitionService which then holds onto the message array key and then in the View I retrieve the message array key from the RecognitionService and use it to display the correct message but I have a feeling there is a better way of doing it, any ideas?

Thanks.

  • 写回答

1条回答 默认 最新

  • douyou7102 2013-03-24 10:28
    关注

    The controller and current view should share the same factory for instantiating services, which ensures that each service is initialized only once.

    From controller:

    public function postLogin( $request )
    {
        $recognition = $this->serviceFactory->create('Recognition');
        $recognition->authenticate( $request->getParameter('username'),
                                    $request->getParameter('password') );
    }
    

    From view:

    public function login()
    {
        $recognition = $this->serviceFactory->create('Recognition');
        if ( $recognition->hasAuthenticationError() )
        {
            header('Location: /login');
        }
    
        if ( $recognition->hasChangedState() ) //was POST, PUT or DELETE call
        {
            header('Location: ./');
        }
    }
    
    public function render()
    {
        // .. make the html/json/whatever
    }
    

    Something like this .. that's my 2 cents. Honestly, how you implement the view is entirely up to you.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 求解vmware的网络模式问题
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥30 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?