2

i have two controllers: firstController and SecondController. In the firstController I have to call a function of the secondController, passing the parameters.

How can you do? Thanks

3
  • 2
    Why would you want to do that? Consider using a service injected into both instead. Commented Sep 3, 2013 at 9:44
  • Putting the code into a service would be a good idea, but you can dispatch other controllers: framework.zend.com/manual/2.0/en/modules/… Commented Sep 3, 2013 at 11:11
  • Dispatching is good if what the OP is trying to accomplish is to render a different view. Say render a 404 view on an error. However, if the OP is just trying to access a method that does something with the data then a plugin or a service is the better way to go. Controllers should have very little to no code that is shared with other controllers. Commented Sep 5, 2013 at 4:08

3 Answers 3

7

Calling an action on a controller is called dispatching. You can also dispatch a controller within a controller, which is called forwarding. There is a controller plugin available to forward to another controller.

An example:

class FirstController extends AbstractActionController
{
    public function fooAction()
    {
        $result = $this->forward()->dispatch('SecondController', array(
            'action' => 'bar',
        ));

        // $result is ViewModel with parameter bar = "baz"
    }
}

class SecondController extends AbstractActionController
{
    public function barAction()
    {
        return new ViewModel(array(
            'bar' => 'baz',
        ));
    }
}

If you leave the second argument to dispatch() out, it will use the current parameters of the primary controller.

Sign up to request clarification or add additional context in comments.

2 Comments

its return error like this: Zend\Mvc\Controller\ControllerManager::get was unable to fetch or create an instance for SecondController
You have to define it as a controller. This does falls out of the scope of your question, but check the second code block here: zf2.readthedocs.org/en/latest/modules/…. Create a key in the controllers array with an invokable or factory, which should be enough.
1

i think this is a bad idea to do that

in addition to the redispatching solution, why don't you try to create a service for your need (just a class with needed methods) and use it in your controllers

or a plugin if you will use it frequently in your controllers ( or register event listners and trigger events in your controllers if you need to do a job in a larger scale or anywhere in your app)

however you can get your controller from the controllerLoader and call its methods (but in my opinion, you're beginning spaghetti coding if you can't find any alternative to do that)

Comments

-2

Declare the method as static in SecondController

class SecondController extends AbstractActionController
{
    public static function method1($param) {
        ...
    }
}

Then call it from FirstController as

SecondController::method1($param);

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.