Phalcon PHP: Dispatcher's `forward` function does not work

370 Views Asked by At

Within the reentryAction in my CustomerclientController I want to forward into the indexAction in case of successful validity check of the post parameters.

Unfortunately, the forward doesn't work. While debugging, I determined that the reentryAction method will be called again, instead of indexAction.

I registered a Dispatcher in my services.php as follows:

$di->setShared('dispatcher', function() use ($eventsManager) {

    $dispatcher = new MvcDispatcher();
    // Bind the eventsManager to the view component
    $dispatcher->setEventsManager($eventsManager);
    return $dispatcher;
});

The CustomerclientController:

class CustomerclientController extends Controller {
    public function reentryAction($hash) {
        // ... a lot of code
        if ($this->request->isPost()) {
            // ... a lot of code
            if ($valid) {
                $this->dispatcher->forward([
                    "customerclient",
                    "index"
                ]);
                return;
            }
        }
    }
    public function indexAction() {
        //Does something wise
    }
}

I defined no special routes and do not use a route.php file.

What am I doing wrong or what did I forget?

Thanks in advance for your help!

2

There are 2 best solutions below

2
On BEST ANSWER

Are you sure you need use MvcDispatcher? I checked my project and I am using Dispatcher

$di->set('dispatcher', function() use ($di) {
    $dispatcher = new Dispatcher;
    $dispatcher->setEventsManager($eventsManager);
    return $dispatcher;
}); 

I hope it works!

3
On

First of all, try to return the dispatcher->forward(). Second, maybe write the keys in the array.

if ($valid) 
  return $this->dispatcher->forward([
    "controller" => "customerclient",
    "action" => "index"
  ]);

Hope that helps!