如何在所有控制器 init() 函数中运行相同的行?
我在所有控制器中都需要相同的两行,每个控制器都有自己的初始化逻辑,但这两行对于所有控制器都是通用的。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
只需子类化控制器即可:
您也可以实现相同的编写Controller Plugin。
编辑:
前端控制器插件在每个请求上执行,就像
控制器
一样并且具有相同的挂钩方法:此外,您可以检查控制器参数以仅在选定的请求上执行代码:
Just subclass the controller:
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:I addition, you may check controller params to execute the code only on selected requests:
您可以通过您的视图访问您的闪存消息(您不需要从控制器发送任何内容到您的视图,这都是自动的)
,我还建议您将此代码封装在视图助手中,就像本网站上显示的那样< 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)
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
在你的引导程序中:
In your bootstrap:
编写您自己的自定义控制器,在该控制器的
init
方法中实现它,然后从您的自定义控制器扩展应用中的所有控制器。但是,@Jeff 提到的使用单独视图助手的方法(查看链接)通常被认为是更好的解决方案。
在你的控制器中只做:
视图助手将完成其余的工作。
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:
and view helper will do the rest.
这不是创建新的自定义控制器的原因。只需将此行添加到所有 init() 方法中即可。
It's not the reason for creating new custom controller. Just add this line to all you init() methods.