Zend Framework - 如何知道哪个控制器和哪个特定方法被执行?

时间:2012-02-05 11:29:06

标签: php zend-framework zend-controller

当我执行/ mycontroller / search时,它只显示“/ mycontroller”但是 当我使用search方法时,如何获得“/ mycontroller / search”,当我使用other方法时,如何获得“/ mycontroller / other”。

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

2 个答案:

答案 0 :(得分:4)

$this->getRequest()->getActionName();返回操作名称。 您也可以使用$_SERVER['REQUEST_URI']来获得您想要的内容。

答案 1 :(得分:2)

为什么你会期望 / mycontroller / search

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