使用 Zend Form 在每个页面上显示登录表单 [英] Show login form on every page using 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('')->setSeparator('</div><div class="sidebar-element">')->setPostfix('</div>');$sidebar->append(new Form_Login);$sidebar->append(new Model_Category->getList());返回 $sidebar}}在我的 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('')->setSeparator('</div><div class="sidebar-element">')->setPostfix('</div>');if(Zend_Registry::isReigistered('loginForm')) {$loginForm = Zend_Registry::get('loginForm');} 别的 {$loginForm = new Form_Login();}$sidebar->append($loginForm);$sidebar->append(new Model_Category->getList());返回 $sidebar}}公共函数 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);如果($认证){//成功 - 显示身份而不是 loginForm} 别的 {$this->view->loginForm = $form;$this->render('/index');}}}}
不,如果表单的实例已经存在,则不会进行自我检查.你必须自己做这件事.
我很确定建议的方法是使用视图助手.正如您在问题中所说,使用操作视图助手会导致另一个不利于性能的调度循环.我不知道是否应该将所有逻辑都保留在视图助手中的判断是什么.
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屋!
查看全文
相关文章
- Zend_Form - 同一页面上的多个表单;
- 在每个页面上添加一个django登录表单;
- 在每个页面上放置一个 django 登录表单;
- 在每个页面上登录和注销;
- 在ViewScript中显示Zend_Form表单错误;
- 用户使用表单django在登录页面上选择语言;
- 每个页面上的Laravel 5.5登录表单,刷新而不是重定向;
- Facebook登录:在每个页面上检查身份验证?;
- 使用Xamarin表单在每个页面上显示不同的工具栏按钮;
- 使用jQuery在页面上包装每个*;
- Django:显示在每个页面上加载页面所需的时间;
- Zend_Form 使用子表单getValues() 问题;
- cordova键盘自动显示在每个表单页面;
- Drupal Form:希望在页面上将以前的表单值显示为 default_value;
- 在每个页面上包括菜单;
- 如何使用模板在 WooCommerce 的两个不同页面上显示登录和注册表单;
- 登录页面上如何显示网格大写锁定;
- Jsoup无法在页面上登录;
- 在页面上显示特定的div后,允许在登录表单上按下key 13;
- 在Django的页面上使用多个表单;
- 在 Django 的页面上使用多个表单;
- 在keycloak登录页面上显示应用程序名称;
- 我想在每个页面上显示用户名;
- 在每个页面上包含页眉/页脚?;
- 在页面上显示验证;
PHP最新文章
- 请求头字段Access-Control-Allow-Headers在预检响应中不允许Access-Control-Allow-Headers;
- 路由问题导致Symfony \ Component \ HttpKernel \ Exception \ NotFoundHttpException错误;
- 什么是NCFB和NOFB模式?;
- 警告:mysqli_connect():(HY000 / 1045):访问被拒绝用户'用户名'@'localhost'(使用密码:是);
- 如何处理致命错误:cURL错误7:无法连接到xxxx端口443;
- 参数3传递给GuzzleHttp\Client :: request()必须是数组类型,给定字符串;
- phpMyAdmin的#2054无法登录到MySQL服务器;
- SSL错误SSL3_GET_SERVER_CERTIFICATE:证书验证失败;
- 在PHPExcel中设置字体颜色,字体和字体大小;
- 如何解决cURL错误(7):无法连接到主机?;