Retrieve Current Module/Controller/Action inside View Script / Layout Script

Before I forget to do this again I’m going to make a post on how to get the current Module, Controller, and Action while inside a View Script or Layout Script.

Here is the most correct way to do it:

[php]
$front = Zend_Controller_Front::getInstance();
$request = $front->getRequest();

$module = $request->getModuleName();
$controller = $request->getControllerName();
$action = $request->getActionName();
[/php]

Now, here is the incorrect way I attempted to do this the first time:

[php]
$front = Zend_Controller_Front::getInstance();
$request = $front->getRequest();

$module = $request->getParam(‘module’);
$controller = $request->getParam(‘controller’);
$action = $request->getParam(‘action’);
[/php]

I found some odd behavior because sometimes while in the default module it would return default, but other times it would return a null. I’m not sure if this is a bug or not, but the most sure way to retrieve the desired result is using the appropriate methods of getModuleName(), etc.

Hope this helps someone out there!

1 thought on “Retrieve Current Module/Controller/Action inside View Script / Layout Script

  1. Testing new spam comment filter.

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this:
search previous next tag category expand menu location phone mail time cart zoom edit close