phalconphp访问触发事件的controllerName

时间:2014-04-25 13:42:28

标签: php events phalcon

如何从事件本身引用触发beforeExecuteRoute的controllerName和actionName?

<?php
use Phalcon\Events\Manager as EventsManager;

//Create a events manager
$eventManager = new EventsManager();

//Listen all the application events
$eventManager->attach('micro', function($event, $app) {

    if ($event->getType() == 'beforeExecuteRoute') {
        //how to get controller name to handle acl stuff
    }
});

3 个答案:

答案 0 :(得分:1)

来自文档 - http://docs.phalconphp.com/en/latest/api/Phalcon_Mvc_Dispatcher.html

getModuleName () - Gets the module where the controller class is
getControllerName () - Gets last dispatched controller name
getActionName () - Gets the lastest dispatched action name

示例:

<?php
use Phalcon\Events\Manager as EventsManager;

//Create a events manager
$eventManager = new EventsManager();

//Listen all the application events
$eventManager->attach('micro', function($event, $app) { 
    if ($event->getType() == 'beforeExecuteRoute') {            
        $controllerName = $app->getControllerName();
        $moduleName = $app->getModuleName();
        $actionName = $app->getActionName();          
    }
});

答案 1 :(得分:0)

如果您没有调度程序,则必须从路由器获取这些值。我不太熟悉微型应用程序的细节,但从查看文档来看,它必须是类似的东西。

<?php
use Phalcon\Events\Manager as EventsManager;

//Create a events manager
$eventManager = new EventsManager();

//Listen all the application events
$eventManager->attach('micro', function($event, $app) {

    if ($event->getType() == 'beforeExecuteRoute') {
        //how to get controller name to handle acl stuff

        DI::getDefault()->get('router')->getControllerName();
        DI::getDefault()->get('router')->getActionName();

        // or

        $app->getRouter()->getControllerName();
        $app->getRouter()->getActionName();
    }
});

这有用吗?

答案 2 :(得分:0)

这样你就可以得到字符串格式的路由来解析它:

$router->getMatchedRoute()->getPattern();

希望这有帮助。我找不到其他办法。