Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Redirect dispatcher to another action

Hello everyone.

I'm making my first steps in Phalcon and found a problem.

I'm trying to make controller redirect dispatcher to another action in the same controller:

public function beforeExecuteRoute($dispatcher)
    {
        $action = $dispatcher->getActionName();
        $dispatcher->setActionName($action.'SomeSuffix');
    }

However it does'nt seem to work. So my questions are:

  1. How can I make dispatcher to fire another action instead of action, it would execute? I want to do this in controller, not in routes definition.
  2. What is the setActionName() method for? It does'nt seem to make any difference?

Adam



19.6k

Only wanna a redirect? Then $this->response->redirect("controller/action") // "/action" is optional



2.3k

My mistake. I don't want to execute full redirect with http. I Just want to execute another action instead of the action that would be executed.

Tried $dispatcher->forward() but it loops the dispatcher.



19.6k

If, as you say, tried forward() ( http://docs.phalconphp.com/es/latest/reference/dispatching.html#forwarding-to-other-actions ) and isn't what you want, then I've no idea.

However, I think forwarding is the nearest thing you can do.



2.3k

I tried to do something like this in beforeExecuteRoute():

        $newMethod = 'anotherMethod';
        $this->$newMethod();
        return false;

But then i get no calls to afterExecuteRoute() etc.

So is there any way to change dispathers action inside dispatch loop?



19.6k
Accepted
answer

Maybe creating you own dispatcher... but I'm not sure, we need someone expert in this.



30.7k
edited Sep '14

Hi,

saying I'm in editAction on CategoryController, then I can use:

return $this->dispatcher->forward(array(
    "controller" => "category",
    "action" => "index"
));


2.3k

I thought that except me there are only exprets here. ;)

Yes, creating my own dispatcher would solve the problem but it seems illogical. While i'm in a dispatch loop that will execute controler/action I can add to that loop execution of another controllerA/actionB but I can't do anything with it.

In other words - forward executes the default action and then executes forwarded one. I'd like to execute the forwarded action only.

I imagine that dispatcher has some internal list that it executes. A list of controller-action pairs. I can manipulate that list by adding another pairs but I cant do other manipulations like deleting, skipping items on that list. For example

$dispatcher->skip() - would skip execution of next action. $dispatcher->skipAll() - would skip everything.



2.3k

That works but still executes the editAction. Is'nt there a way to skip it in beforeExecuteRoute()?

Hi,

saying I'm in editAction on CategoryController, then I can use:

```php return $this->dispatcher->forward(array( "controller" => "category", "action" => "index" )); ```



30.7k
edited Sep '14

OK, so if you do not want that (following my example) the editAction is executed, why don't you call directly CategoryController/indexAction ?

That works but still executes the editAction. Is'nt there a way to skip it in beforeExecuteRoute()?

I do not think so. As the documentationsays, it is:

Triggered before executing the controller/action method.

<=> so CategoryController/editAction is executed ...

Edit: maybe you can try "beforeDispatchLoop" or "beforeDispatch" events, but I'm quiet sure that when the dispatcher is called, the requested modulee/controller/action/params is executed



2.3k

I guess i'll go with custom dispatcher. Still it seems illogical not to be able to manipulate dispatch loop from inside the controler when you could.