15

I know to how to get controller name and action name from layout.phtml file so that i can make dynamic css.

4 Answers 4

45

Try this:

Zend_Controller_Front::getInstance()->getRequest()->getControllerName();
Zend_Controller_Front::getInstance()->getRequest()->getActionName();
Sign up to request clarification or add additional context in comments.

2 Comments

You can also get the module name the same way Zend_Controller_Front::getInstance()->getRequest()->getModuleName()
I'm getting error as ouput for controllername and actioname, what might be wrong?
6

You shouldn't really have logic in your layout.

Best inject your css from your controller using the headlink container.

So in your controller...

$this->view->headLink()->appendStylesheet('custom_stylesheet.css');

And in your layout...

echo $this->headLink();

Simple as that! :)

6 Comments

I tend to think that the view should be setting the view assets such as stylesheets, scripts, etc, and not the controller. Using the placeholder view helpers is the way to go though.
I, on the other hand, tend to think that the view should be a canvas for the designers to... eerm design and that you should only try and echo stuff. Generic stuff should be defined in the init() method of the controller and specific stuff should be defined within the actions...
@AngelP What about where you use a context switch resulting in different view scripts for the same action? Surely you don't want to add stylesheets to the headLink placeholder if they're not going to be used. Hence, adding view assets in the view is the best place for it.
Different view scripts for the same action? Hmmmm... I like to keep things simple so I'd rather have multiple actions with their respective views to be honest. But that's me. ;) Of course, different applications need different solutions - so I'm not saying my solution is the right one, it's just the one I would follow...
@AngelP You've never used the magic of context switching, especially the AjaxContext helper? You're seriously missing out ;)
|
0

for finding controller name and action name in zend use this in controller

   Zend_Controller_Front::getInstance()->getRequest()->getControllerName()
   Zend_Controller_Front::getInstance()->getRequest()->getActionName()

Comments

-1
<?php 
echo $this->headLink()->appendStylesheet($this >baseUrl().'path to your css file without public folder'); 
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.