Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8461471
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T13:53:23+00:00 2026-06-10T13:53:23+00:00

in zf1, we can get controller and action name using $controller = $this->getRequest()->getControllerName(); $action

  • 0

in zf1, we can get controller and action name using

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

How we can achieve this in zf2?

UPDATE:
I tried to get them using

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

But I am getting error

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

I like to get them in __construct() method;

Ideally I would like to check if there is no Action is defined it will execute noaction() method. I would check using php method method_exists.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T13:53:25+00:00Added an answer on June 10, 2026 at 1:53 pm

    you can’t access these variables in controller __construct() method, but you can access them in dispatch method and onDispatch method.

    but if you would like to check whether action exist or not, in zf2 there is already a built in function for that notFoundAction as below

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

    but if you still like to do it manually you can do this using dispatch methods as follow

    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';   
        }
    }   
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am learning ZF2.0 beta . In ZF1 we can change layout on the
With ZF1 there was a bootstrap file defined under application/Bootstrap.php but I can't find
I have the following function in ZF1 login controller and works fine but cannot
I think I've tried everything I've found to solve this, including all the answers
ZF 1.11.2 here. I have something like this in my controller: $validators = array(
I'm trying to use modules in the ZF 1.8 project but can't get working
I used below code to render a page in the controller action. public function
In ZF1 we use partial in layout.phtml file something like that $this->partial('header.phtml', array('vr' =>
I would like to create a generic module/controller/action route in Zend Framework 2 to
can anybody help me how to nice integrate doctrine 2.0 into ZF 1.10? I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.