如何在所有控制器 init() 函数中运行相同的行?

发布于 2024-10-18 06:12:48 字数 792 浏览 2 评论 0原文

我在所有控制器中都需要相同的两行,每个控制器都有自己的初始化逻辑,但这两行对于所有控制器都是通用的。

public function init()
{
    $fm =$this->_helper->getHelper('FlashMessenger');
    $this->view->messages = $fm->getMessages();
}

如何避免重复代码?

更新:

好的,FlashMessenger 只是一个示例,假设我需要在除“someAction”@“someController”之外的每个操作中写入日志行。所以新的共同点应该是。

$this->logger = new Zend_Log();
$writer = new Zend_Log_Writer_Stream(APPLICATION_PATH.'/../logs/log.txt');
$this->logger->addWriter($writer);
$this->logger->log('Some Message',Zend_Log::DEBUG);

问题是,我应该将这些行放在哪里,以避免在每个控制器的所有 init() 中重复它们。 这些行应该放在 bootstrap? 处。如果是这样:如何跳过“someAction”的日志行。 或者我应该实现一个“BaseController”并让我的所有控制器都从它扩展。如果是这样:我怎样才能自动加载它? (致命错误:未找到“BaseController”类)。

I need same 2 lines in all my controllers, each controller have its own init logic, but these two lines are common for all of them.

public function init()
{
    $fm =$this->_helper->getHelper('FlashMessenger');
    $this->view->messages = $fm->getMessages();
}

How can I avoid repeat code ?

Update:

Ok, the FlashMessenger was only an example, let's say I need write a log line in every action except for 'someAction' @ 'someController'. So the new common lines should be.

$this->logger = new Zend_Log();
$writer = new Zend_Log_Writer_Stream(APPLICATION_PATH.'/../logs/log.txt');
$this->logger->addWriter($writer);
$this->logger->log('Some Message',Zend_Log::DEBUG);

The question is, where should I place these lines in order to avoid repeat them in all init() of each controller.
These lines should be placed at bootstrap?. If so: How can skip log lines for 'someAction'.
Or should I implement a 'BaseController' and make all my controller extend from it. If so: How can I Autoload it? (Fatal error: Class 'BaseController' not found) .

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

逆夏时光 2024-10-25 06:12:48

只需子类化控制器即可:

class Application_ControllerAction extends Zend_Controller_Action {
    public function init()
    {
        $fm =$this->_helper->getHelper('FlashMessenger');
        $this->view->messages = $fm->getMessages();
    }
}


class IndexController extends Application_ControllerAction {
}

您也可以实现相同的编写Controller Plugin。

编辑:

前端控制器插件在每个请求上执行,就像控制器一样并且具有相同的挂钩方法:

routeStartup(): prior to routing the request
routeShutdown(): after routing the request
dispatchLoopStartup(): prior to entering the dispatch loop
preDispatch(): prior to dispatching an individual action
postDispatch(): after dispatching an individual action
dispatchLoopShutdown(): after completing the dispatch loop

此外,您可以检查控制器参数以仅在选定的请求上执行代码:

if ('admin' == $this->getRequest()->getModuleName() 
&& 'update' == $this->getRequest()->getActionName() ) …

Just subclass the controller:

class Application_ControllerAction extends Zend_Controller_Action {
    public function init()
    {
        $fm =$this->_helper->getHelper('FlashMessenger');
        $this->view->messages = $fm->getMessages();
    }
}


class IndexController extends Application_ControllerAction {
}

You may also achieve the same writing Controller Plugin.

Edit:

Front controller plugins are executed on each request, just like the Controllers and have the same hook methods:

routeStartup(): prior to routing the request
routeShutdown(): after routing the request
dispatchLoopStartup(): prior to entering the dispatch loop
preDispatch(): prior to dispatching an individual action
postDispatch(): after dispatching an individual action
dispatchLoopShutdown(): after completing the dispatch loop

I addition, you may check controller params to execute the code only on selected requests:

if ('admin' == $this->getRequest()->getModuleName() 
&& 'update' == $this->getRequest()->getActionName() ) …
轻许诺言 2024-10-25 06:12:48

您可以通过您的视图访问您的闪存消息(您不需要从控制器发送任何内容到您的视图,这都是自动的)

$fm = new Zend_Controller_Action_Helper_FlashMessenger();
Zend_Debug::dump($fm->getMessages());

,我还建议您将此代码封装在视图助手中,就像本网站上显示的那样< a href="http://grummfy.be/blog/191" rel="nofollow">http://grummfy.be/blog/191

You can access your flash messages through (you dont need to send anything from your controller to your view, it's all automated)

$fm = new Zend_Controller_Action_Helper_FlashMessenger();
Zend_Debug::dump($fm->getMessages());

in you view, i would also recommand that you encapsulate this code in a view helper like it is shown on this site http://grummfy.be/blog/191

残疾 2024-10-25 06:12:48

在你的引导程序中:

protected function _initMyActionHelpers() {
    $fm = new My_Controller_Action_Helper_FlashMessenger();
    Zend_Controller_Action_HelperBroker::addHelper($fm);;
}

In your bootstrap:

protected function _initMyActionHelpers() {
    $fm = new My_Controller_Action_Helper_FlashMessenger();
    Zend_Controller_Action_HelperBroker::addHelper($fm);;
}
可是我不能没有你 2024-10-25 06:12:48

如何避免重复代码?

编写您自己的自定义控制器,在该控制器的 init 方法中实现它,然后从您的自定义控制器扩展应用中的所有控制器。

但是,@Jeff 提到的使用单独视图助手的方法(查看链接)通常被认为是更好的解决方案。
在你的控制器中只做:

$this->_helper->flashMessanger('My message');

视图助手将完成其余的工作。

How can I avoid repeat code ?

Write your own custom controller, implement that in init method of that controller, and then extend all controllers in your app from your custom controller.

But approach with separate view helper as @Jeff mentioned (look at link) is often taken as a better solution.
In your controller do only:

$this->_helper->flashMessanger('My message');

and view helper will do the rest.

天荒地未老 2024-10-25 06:12:48

这不是创建新的自定义控制器的原因。只需将此行添加到所有 init() 方法中即可。

$this->view->messages = $this->_helper->getHelper('FlashMessenger')->getMessages();

It's not the reason for creating new custom controller. Just add this line to all you init() methods.

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