We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Phalcon\Http\Response redirect, Phalcon 2 vs Phalcon 1

Hi people,

Sorry to bother you again. I would like to confirm that in Phalcon 2.0.6 if redirecting within the same controller there should be a forward-slash at the beginning of the string. For example:

<?php

class SystemUsersController extends ControllerBase{
// ......
    $this->response->redirect('/systemUsers/action');
    /* instead of 
    $this->response->redirect('systemUsers/action'); // work for Phalcon 1.3, doesn't work for Phalcon 2.0.6
    */

When I was using Phalcon 1.3, it was fine without said forward-slash. But after I upgrade my Phalcon to 2.0.6, if I do not have the forward-slash at the beginning of the redirect string, it'd actually redirect to 'systemUsers/systemUsers/action', which is prepending the controller itself.

Yo, it should be working without:a preceding forward slash:

  //Using a string redirect (internal/external)
$response->redirect("posts/index");
$response->redirect("https://en.wikipedia.org", true);
$response->redirect("https://www.example.com/new-location", true, 301);

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

https://docs.phalcon.io/en/latest/api/Phalcon_Http_Response.html



25.7k

When I have it redirecting to itself, it doesn't work.

Yo, it should be working without:a preceding forward slash:

 //Using a string redirect (internal/external)
$response->redirect("posts/index");
$response->redirect("https://en.wikipedia.org", true);
$response->redirect("https://www.example.com/new-location", true, 301);

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

https://docs.phalcon.io/en/latest/api/Phalcon_Http_Response.html