ZendFramework - How to know which controller and which specific method is execued?
-
22-04-2021 - |
Pregunta
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;
}
}
Solución
$this->getRequest()->getActionName();
returns action name.
you also may use $_SERVER['REQUEST_URI']
to get what you want.
Otros consejos
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;
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow