2011-07-15 120 views
7

可能是一個荒謬的問題,但是有沒有從控制器類本身獲取實際控制器名稱的方法?從控制器本身獲取控制器名稱

class SomeController extends Zend_Controller_Action { 
    public function init() { 

     $controllerName = $this -> getControllerName(); 
     // And get "Some" as a output 
    } 
} 
+5

like get_class($ this)? – Rufinus

+0

@Rufinus,正好! :) – mrN

回答

11
public function init() { 
    echo Zend_Controller_Front::getInstance()->getRequest()->getControllerName(); 
} 
+0

哇,看起來很多要求。它是否加起來與網站的性能? – mrN

5

您可以要求使用getControllerName()得到控制器的名稱。要獲得請求(無單例),您可以執行以下操作:

public function init() { 
    $controllerName = $this->_request->getControllerName(); 
    // or 
    $controllerName = $this->getRequest()->getControllerName(); 
    // or 
    $controllerName = $this->getFrontController()->getRequest()->getControllerName() 
} 
+0

最佳答案!爲我工作! – Abadis