ZendFramework - How to know which controller and which specific method is execued?

92 views Asked by At

When i execute /mycontroller/search it shows only "/mycontroller" but how do i get "/mycontroller/search" when i am in search method, how do i get "/mycontroller/other" when i am in other method.

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

There are 2 answers

0
heximal On BEST ANSWER

$this->getRequest()->getActionName(); returns action name. you also may use $_SERVER['REQUEST_URI'] to get what you want.

0
RockyFord On

why would you expect /mycontroller/search from this:

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

you are only asking for the controller.

this would work:

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