gpt4 book ai didi

法尔康 : get instance of controller in beforeExecuteRoute

转载 作者:行者123 更新时间:2023-12-01 12:39:01 26 4
gpt4 key购买 nike

如何在 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。另一种选择是在您的 beforeExecuteRoutebeforeNotFoundAction 事件中添加该跟踪,挖掘 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com