2012-02-05 61 views
2

當我執行/ mycontroller /搜索它只顯示「/ mycontroller」,但 當我在search方法時如何獲得「/ mycontroller/search」,當我處於other時,如何獲得「/ mycontroller/other」方法。ZendFramework - 如何知道哪個控制器和哪個特定的方法被執行?

class Mycontroller extends Zend_Controller_Action 
{ 
    private $url = null; 
    public function otherAction() { 
    $this->url .= "/" . $this->getRequest()->getControllerName(); 
    echo $this->url; // output: /mycontroller 
    exit; 
    } 
    public function searchAction() { 
    $this->url .= "/" . $this->getRequest()->getControllerName(); 
    echo $this->url; // output: /mycontroller 
        // expect: /mycontroller/search 
    exit; 
    } 
} 

回答

4

$this->getRequest()->getActionName();返回操作名稱。 你也可以使用$_SERVER['REQUEST_URI']來得到你想要的。

2

爲什麼你會期望從這一/myController的/搜索

public function searchAction() { 
    $this->url .= "/" . $this->getRequest()->getControllerName(); 
    echo $this->url; // output: /mycontroller 
        // expect: /mycontroller/search 
    exit; 
    } 

你只是要求控制器。

這會工作:

public function searchAction() { 
    $this->url = '/'. $this->getRequest()->getControllerName(); 
    $this->url .= '/' . $this->getRequest()->getActionName(); 

    echo $this->url; // output: /mycontroller/search 

    echo $this->getRequest()->getRequestUri(); //output: requested URI for comparison 

    //here is another way to get the same info 
    $this->url = $this->getRequest()->controller . '/' . $this->getRquest()->action; 
    echo $this->url; 

exit; 
} 
相關問題