Redirection in Phalcon

Hello everyone.I have got 3 questions about redirection in Phalcon. 1) Is it correct to set my baseUri to '/' if I'm working on a localhost and it is pointing to the public folder and I am accessing my website thru phalcon.loc in my browser. 2) What is the difference between redirecting

    $this->response->redirect('index'); //$this = controller

and

    $this->dispatcher->forward (['controller' => 'index']);

3) How to redirect correctly with response->redirect to for example User controller and succssfulLogin action from User controller login action What I have is

    $this->response->redirect('/user/successfulLogin');

and what I get is user/successfulLogin in my browser. Basicly, what I am asking is how to redirect to another action or another controller and action via $this->response->redirect(); Thanks

if your project is at http://localhost then its perfectly right to set baseUri to "/" . if its http://localhost/angay9 then your baseUri should be "/angay9/".

your redirects: 1) makes new http request and redirects setting 302 status code 2) doesnt make new request but "forwards" execution flow to another controller/action in your application

Example of using redirect from response service based on named route: (taken from docs http://docs.phalconphp.com/en/latest/reference/response.html#making-redirections)

<?php

//Redirect based on a named route return $response->redirect(array( "for" => "index-lang", "lang" => "jp", "controller" => "index" ));

If you watch your URL in both cases you will see that forward doesn't change.



17.6k
edited May '14

Thaks for the explanation, but I don't get what is this "for" in the redirect method. I have tried to do something like :

$this->response->redirect(array('controller' => 'user', 'action' => 'successfulLogin'));

But it says is requires this "for" parameter. What's the alternative?

edited May '14

Try a string $this->response->redirect("user\successfulLogin"); instead.