作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
如何在 phalcon
框架的 beforeExecuteRoute
方法中获取令人兴奋的 Controller 实例?
最佳答案
令人兴奋的 Controller ?这完全取决于是什么让他们兴奋……
但是,srsly,您只能从调度程序中获取事件 Controller 的实例,可以像这样访问它:
$controller = $di->getShared('dispatcher')->getActiveController();
如果您将事件处理程序与事件管理器一起使用,则如下所示:
$eventManager->attach("dispatch:beforeExecuteRoute", function (Event $event, Dispatcher $dispatcher) {
$controller = $dispatcher->getActiveController();
});
如果您实际上将现有 Controller 表示为复数,那么您需要为 Controller 实例化添加某种跟踪。您不能通过 Controller 类中的 __construct
执行此操作,因为出于某种原因某些天才将 __construct
标记为 final
。另一种选择是在您的 beforeExecuteRoute
和 beforeNotFoundAction
事件中添加该跟踪,挖掘 the code in the repository详情:
// Exciting controllers get stored whenever the dispatcher reports if the action was not found
// or when it's ready to dispatch. Note, if a controller gets created outside the dispatcher
// it will not be tracked, though in real life that should never happen.
$controller = [];
$eventManager->attach("dispatch:beforeNotFoundAction", function (Event $event, Dispatcher $dispatcher) {
$controllers[] = $dispatcher->getActiveController();
});
$eventManager->attach("dispatch:beforeExecuteRoute", function (Event $event, Dispatcher $dispatcher) {
$controllers[] = $dispatcher->getActiveController();
});
关于法尔康 : get instance of controller in beforeExecuteRoute,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26803239/
如何在 phalcon 框架的 beforeExecuteRoute 方法中获取令人兴奋的 Controller 实例? 最佳答案 令人兴奋的 Controller ?这完全取决于是什么让他们兴奋……
我是一名优秀的程序员,十分优秀!