使用 Zend Form 在每个页面上显示登录表单 [英] Show login form on every page using Zend Form

查看:28
本文介绍了使用 Zend Form 在每个页面上显示登录表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎无法弄清楚如何创建包含登录表单 (Zend_Form) 和各种模块/控制器特定链接的灵活侧边栏.

最常见的解决方案似乎是使用:

echo $this->action('login', 'authentication', 'default');

但这显然不是最佳"方式?我读过这会明显地触发调度循环,从而影响性能?

我想过为侧边栏唱一个 View_Helper:

class Zend_View_Helper_Sidebar 扩展 Zend_View_Helper_Abstract{公共功能侧边栏(){$sidebar = $this->view->placeholder('sidebar');$sidebar->setPrefix('

在我的 Form_Login 中,我将操作设置为/auth/login,其中包含以下代码:

公共函数 loginAction(){$request = $this->getRequest();if($request->isPost()) {$form = new Form_Login();$data = $request->getPost();if($form->isValid($data)) {$username = $form->getValue('username');$password = $form->getValue('password');$users = new Model_DbTable_Users();$authenticated = $users->login($username, $password);如果($认证){//成功 - 显示身份而不是 loginForm} 别的 {$this->view->loginForm = $form;$this->render('/index');}}}}

如果我提供了错误的用户名/密码,它会呈现当前为空的 indexAction.这可以.它还根据需要呈现包含 loginForm 的侧边栏,但表单为空(未显示用户输入.也没有消息表明表单无法验证).

表单中的用户名字段应显示用户在提交前提供的输入.并应显示错误消息.

关于为什么这不发生的任何帮助,非常感谢.

解决方案

您正在 loginAction 中进行表单验证,并且您说您当前正在发布到索引操作,这是一个错字吗?

除此之外,您还要创建 Form_Login 的两个副本.一次,在动作中,一次在视图中.如果您在操作中的一个实例上进行验证,则需要在视图助手中显示该表单的同一实例.您可以将所有验证逻辑移动到视图助手中,也可以在视图助手和操作之间共享实例.我会建议后者.

如果注册表中已存在表单,请检查您的视图助手.如果是这样,只需使用该实例.否则,您可以创建一个新实例.这是我的意思的粗略示例:

class Zend_View_Helper_Sidebar 扩展 Zend_View_Helper_Abstract{公共功能侧边栏(){$sidebar = $this->view->placeholder('sidebar');$sidebar->setPrefix('

不,如果表单的实例已经存在,则不会进行自我检查.你必须自己做这件事.

我很确定建议的方法是使用视图助手.正如您在问题中所说,使用操作视图助手会导致另一个不利于性能的调度循环.我不知道是否应该将所有逻辑都保留在视图助手中的判断是什么.

I cant seem to figure out how to create a flexible sidebar containing and login form (Zend_Form) and various module/controller specific links.

The most common solution seems to be using:

echo $this->action('login', 'authentication', 'default');

But apperently this isnt the 'best' way? I've read that this apprently triggers a dispatchloop and thereby take a performance hit?

I've thought about sing a View_Helper for the sidebar:

class Zend_View_Helper_Sidebar extends Zend_View_Helper_Abstract
{
    public function sidebar()
    {
        $sidebar = $this->view->placeholder('sidebar');
        $sidebar->setPrefix('<div class="sidebar-element">')
                ->setSeparator('</div><div class="sidebar-element">')
                ->setPostfix('</div>');

        $sidebar->append(new Form_Login);
        $sidebar->append(new Model_Category->getList());
        return $sidebar
    }
}

In my Form_Login i have action set to /auth/login which contains the following code:

public function loginAction()
{
    $request = $this->getRequest();

    if($request->isPost()) {
        $form = new Form_Login();
        $data = $request->getPost();
        if($form->isValid($data)) {
            $username = $form->getValue('username');
            $password = $form->getValue('password');
            $users = new Model_DbTable_Users();
            $authenticated = $users->login($username, $password);
            if($authenticated) {                    
                    //Succes - show identity instead of loginForm                   
            } else {                                      
                $this->view->loginForm = $form;
                $this->render('/index');
            }
        }
    }
}

If I provide the wrong username/password it renders indexAction which is currently empty. This is fine. It also renders my sidebar containing the loginForm as needed, but the form is empty (The user input is not displayed. Neither is no message that the form failed to validate).

The username-field in the form should display the input that the user provided before submitting. And a error message should be displayed.

Any help as to why this is not happing, is very much appriciated.

解决方案

You are doing your form validation in a loginAction and you said you are currently posting to index action, is that a typo?

Besides that, you are creating two copies of the Form_Login. Once, in the action and once in view helper. If you validate on one instance in the action, you need to display that same instance of the form in the view helper. You could either move all the validation logic into the view helper or you could share the instance between the view helper and the action. I'm going to suggest the latter.

Check in your view helper if a form already exists in the registry. If it does, just use that instance. Otherwise you can create a new instance. Here's a rough example of what I mean:

class Zend_View_Helper_Sidebar extends Zend_View_Helper_Abstract
{
    public function sidebar()
    {
        $sidebar = $this->view->placeholder('sidebar');
        $sidebar->setPrefix('<div class="sidebar-element">')
                ->setSeparator('</div><div class="sidebar-element">')
                ->setPostfix('</div>');

        if(Zend_Registry::isReigistered('loginForm')) {
            $loginForm = Zend_Registry::get('loginForm');
        } else {
            $loginForm = new Form_Login();
        }

        $sidebar->append($loginForm);
        $sidebar->append(new Model_Category->getList());
        return $sidebar
    }
}

public function loginAction()
{
    $form = new Form_Login();
    Zend_Registry::set('loginForm', $form);

    $request = $this->getRequest();

    if($request->isPost()) {        
        $data = $request->getPost();
        if($form->isValid($data)) {
            $username = $form->getValue('username');
            $password = $form->getValue('password');
            $users = new Model_DbTable_Users();
            $authenticated = $users->login($username, $password);
            if($authenticated) {                    
                    //Succes - show identity instead of loginForm                   
            } else {                                      
                $this->view->loginForm = $form;
                $this->render('/index');
            }
        }
    }
}

Edit:

No, there is no self checking if an instance of a form already exists. You must do this yourself.

I'm pretty sure the suggested way is to use a view helper. As you said in your question, using the action view helper causes another dispatch loop which is bad for performance. I don't know what the verdict is on whether all logic should be kept in the view helper or not.

这篇关于使用 Zend Form 在每个页面上显示登录表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆