views:

64

answers:

2

This is what I want to do:

class IndexController extends Zend_Controller_Action
{
    public function indexAction()
    {
        //some code
        $this->view->var = 'something';
    }

    public function differentAction()
    {
        //here, I want to execute the indexAction, but render the differentAction view script
    }
}

How can I execute the code from a different action, but render the current view script? I want the view variable to apply to the differentAction view script.

+4  A: 

Apparently it's as easy as calling $this->indexAction(). =/

class IndexController extends Zend_Controller_Action
{
    public function indexAction()
    {
        //some code
        $this->view->var = 'something';
    }

    public function differentAction()
    {
        $this->indexAction(); //still renders the differentAction() view script.
    }
}
Andrew
If the variable is required for all of the `IndexController`'s actions, you could also assign the view variable in an `init()` method http://framework.zend.com/manual/en/zend.controller.action.html#zend.controller.action.initialization
Richard Nguyen
that's a good idea. forgot about that. =]
Andrew
+1  A: 

You might also consider making a private / protected method:

class IndexController extends Zend_Controller_Action
{
    private function _sharedCode
    {
        $this->view->var = 'something';
    }

    public function indexAction()
    {
        $this->_sharedCode();
    }

    public function differentAction()
    {
        $this->_sharedCode();
    }
}
smack0007
I actually ended up doing this anyway. Decided it was better than keeping the code in the controller action
Andrew