2014-04-25 35 views
0

我怎樣才能引用從事件本身觸發beforeExecuteRoute的controllerName和actionName?phalconphp觸發事件的訪問控制器名稱

<?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 
    } 
}); 

回答

0

這樣你可以在字符串格式的路徑解析它:

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

希望這有助於。我發現沒有其他的方式來做到這一點。

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();   
    } 
}); 
+0

我的問題依賴於$ app是Phalcon \ Mvc \ Micro的事實,所以我沒有Dispatcher,我可以通過$ app-> getRouter() - > getMatchedRoute()方法返回NULL,如http://docs.phalconphp.com/en/latest/api/Phalcon_Mvc_Router.html中所述,它也應該工作! – diegochaves

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(); 
    } 
}); 

這是行得通嗎?

相關問題