Andrew
Andrew

Reputation: 238647

Zend Framework: How to execute the code from a different action?

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.

Upvotes: 0

Views: 722

Answers (2)

smack0007
smack0007

Reputation: 11366

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();
    }
}

Upvotes: 1

Andrew
Andrew

Reputation: 238647

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.
    }
}

Upvotes: 4

Related Questions