Zend 框架所有控制器的通用代码

发布于 2024-07-25 02:03:10 字数 364 浏览 8 评论 0原文

我在网站的标题中有一个登录按钮。 该标头的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

苍景流年 2024-08-01 02:03:10

创建您自己的扩展 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...

泛泛之交 2024-08-01 02:03:10

请小心 Pradeep Sharma 的解决方案(他自己写的答案并在下面接受)。

下面的所有代码均适用于 ZF 1.12,而不是 ZF 2.0

在引导程序中,Zend_Layout 的 MVC 实例可能尚未创建。 您应该使用 Zend_Layout::startMvc() 来代替:

$view = Zend_Layout::startMvc()->getView() ;

而且我更喜欢在 preDispatch() 函数中执行此代码。 ZF 的新用户可能对此感兴趣:

application/plugins/HeaderForm.php:

class Application_Plugin_HeaderForm extends Zend_Controller_Plugin_Abstract
{
    public function preDispatch(Zend_Controller_Request_Abstract $request)
    {    
        $view = Zend_Layout::startMvc()->getView() ;
        $view->headerForm = new Application_Form_HeaderForm() ;
    }
}

调用新的 Application_Form_HeaderForm() 将默认自动加载到 application/forms/ 文件夹中。 您还可以使用 new Zend_Form()addElement() 等直接在插件中创建表单,但它不可重用。

当然,你需要在你的引导程序中注册这个插件!

application/Bootstrap.php :

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
    protected function _initPlugin()
    {
        $front = Zend_Controller_Front::getInstance() ;
        $front->registerPlugin(new Application_Plugin_HeaderForm()) ;
    }
}

调用新的 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 :

$view = Zend_Layout::startMvc()->getView() ;

And tbh I prefer executing this code in the preDispatch() function. New users of ZF might be interested in this :

application/plugins/HeaderForm.php :

class Application_Plugin_HeaderForm extends Zend_Controller_Plugin_Abstract
{
    public function preDispatch(Zend_Controller_Request_Abstract $request)
    {    
        $view = Zend_Layout::startMvc()->getView() ;
        $view->headerForm = new Application_Form_HeaderForm() ;
    }
}

Calling new Application_Form_HeaderForm() will autoload by default into application/forms/ folder. You can also create the form directly into the plugin with new Zend_Form(), and addElement() etc. but it won't be reusable.

Of course, you need to register this plugin in your bootstrap!

application/Bootstrap.php :

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
    protected function _initPlugin()
    {
        $front = Zend_Controller_Front::getInstance() ;
        $front->registerPlugin(new Application_Plugin_HeaderForm()) ;
    }
}

Calling new Application_Plugin_HeaderForm() will autoload by default into application/plugins/ folder

我爱人 2024-08-01 02:03:10

我以不同的方式完成了它,扩展Zend_Controller_Plugin_Abstract来实现插件并将其注册到前端控制器。

public function routeStartup(Zend_Controller_Request_Abstract $request) { } 

在上述方法中并通过在 $view 对象中设置表单来生成表单。

$view 可以使用以下方式检索:

$view = Zend_Layout :: getMvcInstance()->getView();

I did it in a different way, extendingZend_Controller_Plugin_Abstract to implement a plugin and register it with front controller.

public function routeStartup(Zend_Controller_Request_Abstract $request) { } 

generated the form inside the above mentioned method and by setting the form in $view object.

$view can be retrived using :

$view = Zend_Layout :: getMvcInstance()->getView();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文