How to get controller name and actions in zf2

in zf1, we can get the name of the controller and action using

$controller = $this->getRequest()->getControllerName();
$action = $this->getRequest()->getActionName();

How can we achieve this in zf2?

UPDATE: I tried to get them to use

echo $this->getEvent()->getRouteMatch()->getParam('action', 'NA');
echo $this->getEvent()->getRouteMatch()->getParam('controller', 'NA');

But I get an error

Fatal error: Call to a member function getParam() on a non-object

I like to use them in the __construct () method;

Ideally, I would like to check if any action is defined, it will execute the noaction () method. I would check using method_exists php method.

+5
source share
4 answers

you cannot access these variables in the controller method __construct(), but you can access them in the dispatchand method onDispatch.

, , zf2 notFoundAction,

 public function notFoundAction()
{
    parent::notFoundAction();
    $response = $this->getResponse();
    $response->setStatusCode(200);
    $response->setContent("Action not found");
    return $response;   
} 

, ,

namespace Mynamespace\Controller;

use Zend\Mvc\Controller\AbstractActionController;

use Zend\Stdlib\RequestInterface as Request;
use Zend\Stdlib\ResponseInterface as Response;
use Zend\Mvc\MvcEvent;

class IndexController extends AbstractActionController 
{

    public function __construct()
    {


    }        

      public function notFoundAction()
    {
        parent::notFoundAction();
        $response = $this->getResponse();
        $response->setStatusCode(200);
        $response->setContent("Action not found");
        return $response;   
    }

    public function dispatch(Request $request, Response $response = null)
    {
        /*
         * any customize code here
         */

        return parent::dispatch($request, $response);
    }
    public function onDispatch(MvcEvent $e)
    {
        $action = $this->params('action');
        //alertnatively 
        //$routeMatch = $e->getRouteMatch();
        //$action = $routeMatch->getParam('action', 'not-found');

        if(!method_exists(__Class__, $action."Action")){
           $this->noaction();
        }

        return parent::onDispatch($e);
    }
    public function noaction()
    {        
        echo 'action does not exits';   
    }
}   
+4

:

$controllerName =$this->params('controller');
$actionName = $this->params('action');
+12

You will get a module, controller and action name similar to this in Zf2 inside your controller ...

$controllerClass = get_class($this);
$moduleNamespace = substr($controllerClass, 0, strpos($controllerClass, '\\'));
$tmp = substr($controllerClass, strrpos($controllerClass, '\\')+1 );
$controllerName = str_replace('Controller', "", $tmp);

//set 'variable' into layout...
$this->layout()->currentModuleName      = strtolower($moduleNamespace);
$this->layout()->currentControllerName  = strtolower($controllerName);
$this->layout()->currentActionName      = $this->params('action');
0
source
$controllerName = strtolower(Zend_Controller_Front::getInstance()->getRequest()->getControllerName());
-6
source

All Articles