Zend 框架所有控制器的通用代码
我在网站的标题中有一个登录按钮。 该标头的 html 被编程到 Zend 框架的views/layouts/home.phtml 中。
我在此布局中有一个隐藏表单,由 jQuery 厚盒内联内容显示集成触发。 原因是,我不想进行 ajax 调用来获取一个小的登录表单。
我使用 Zend_Form 创建表单,问题是我必须在检查用户是否登录后在所有控制器中执行此操作。 我想将此表单生成放在一个位置,例如在引导程序中,然后在引导程序中使用逻辑来表示如果用户登录,则不生成表单。
我不知道 bootstrap 是否是正确的地方,或者我应该在其他地方这样做。
那么,我应该在哪里实例化表单,以便在用户未登录时它在任何地方都可用。
I have a login button in the header of the website. This header's html is programmed into Zend framework views/layouts/home.phtml.
I have a hidden form in this layout that is triggered by jQuery thickbox inline content display integration. Reason, I dont want to make a ajax call to just fetch a small login form.
I create the form using Zend_Form and the problem is that I have to do it in all the controllers after checking if the user is logged in or not. I want to place this form generation in one single place, say in bootstrap and then have a logic in bootstrap to say that if user is logged in dont generate the form.
I don't know if bootstrap is the right place to do so or should I do it in some other place.
So, where should I instantiate the form so that its available everywhere if user is not logged in.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
创建您自己的扩展 Zend_Controller_Action 的基本控制器,然后让您的控制器扩展您的基本控制器。 我不知道“jQuery 厚盒内联内容显示集成”是什么......但是您可以将其放入多个部分,具体取决于您何时需要运行代码。 init()、preDispatch()、postDispatch() 等...只需确保当您扩展基本控制器时执行以下操作:
parent::init()
父级::preDispatch()
父级::postDispatch()
等等...在每个部分中,以便基本代码也运行...
Create your own base controller which extends Zend_Controller_Action then have your controllers extend off of your base controller. I don't know what "jQuery thickbox inline content display integration" is...but you have several sections you can put it in depending when you need your code to run. init(), preDispatch(), postDispatch() etc... Just make sure when you extend off your base controller that you do sthing like:
parent::init()
parent::preDispatch()
parent::postDispatch()
etc... within each section so that the base code runs as well...
请小心 Pradeep Sharma 的解决方案(他自己写的答案并在下面接受)。
下面的所有代码均适用于 ZF 1.12,而不是 ZF 2.0
在引导程序中,Zend_Layout 的 MVC 实例可能尚未创建。 您应该使用 Zend_Layout::startMvc() 来代替:
而且我更喜欢在 preDispatch() 函数中执行此代码。 ZF 的新用户可能对此感兴趣:
application/plugins/HeaderForm.php:
调用新的
Application_Form_HeaderForm()
将默认自动加载到 application/forms/ 文件夹中。 您还可以使用new Zend_Form()
和addElement()
等直接在插件中创建表单,但它不可重用。当然,你需要在你的引导程序中注册这个插件!
application/Bootstrap.php :
调用新的
Application_Plugin_HeaderForm()
将默认自动加载到 application/plugins/ 文件夹中Be careful about Pradeep Sharma's solution (the answer he wrote himself and accepted below).
All the code code below is for ZF 1.12, and not ZF 2.0
In the bootstrap, Zend_Layout's MVC instance might not have been created yet. You should use
Zend_Layout::startMvc()
instead :And tbh I prefer executing this code in the preDispatch() function. New users of ZF might be interested in this :
application/plugins/HeaderForm.php :
Calling new
Application_Form_HeaderForm()
will autoload by default into application/forms/ folder. You can also create the form directly into the plugin withnew Zend_Form()
, andaddElement()
etc. but it won't be reusable.Of course, you need to register this plugin in your bootstrap!
application/Bootstrap.php :
Calling new
Application_Plugin_HeaderForm()
will autoload by default into application/plugins/ folder我以不同的方式完成了它,扩展
Zend_Controller_Plugin_Abstract
来实现插件并将其注册到前端控制器。在上述方法中并通过在 $view 对象中设置表单来生成表单。
$view 可以使用以下方式检索:
I did it in a different way, extending
Zend_Controller_Plugin_Abstract
to implement a plugin and register it with front controller.generated the form inside the above mentioned method and by setting the form in $view object.
$view can be retrived using :