Slim: Redirect in slim3

Created on 9 Nov 2015  路  14Comments  路  Source: slimphp/Slim

How to redirect to different route in slim 3?

Slim 3 question

Most helpful comment

return $response->withRedirect('/foo');

or:

// inside route callable closure:
$router = $this->router;
return $response->withRedirect($router->pathFor('route-name');

All 14 comments

return $response->withRedirect('/foo');

or:

// inside route callable closure:
$router = $this->router;
return $response->withRedirect($router->pathFor('route-name');

Oh with return - thanks

I don't have the 'withRedirect' function:
I get the following error:
Fatal error: Call to undefined method Slim\Http\Response::wihRedirect()

What did I wrong

Typo. Wit or with

oh sorry my bad
Now it works :+1:

Hi,
//in controller
return $response->withRedirect('/checkParams');
this is not working in controller.
//route
$app->post( '/checkParams', 'ValidatorController:checkParams' );

is there anything more to be done while using in controller?

A redirect is always a GET request. You have made /checkParams a POST, so it won't match.

@akrabat Wow that was lightning fast response. Thanks for super quick response. I will change my route to GET.

@akrabat
I got it working as expected when I turned POST to GET. Now what I am finding challenging is allow cross domain request. Let me explain my scenario in brief.

  1. my web app domain say http://mywebapp.com will call my API end point at http://myapi.com/process/
  2. the internal working of the ProcessController API will be as follows
    a. process method will do some work and will redirect to other controller method ValidatorController:checkParams
    b. ValidatorController:checkParams will check parameters and will redirect to another controller say ExecutorController:executeprocess.

Now if request to API call is from same origin then it works like charm. Complete redirections work as expected and I get final result.
But in case of cross domain call, My ProcessController method gets called but it halts execution where I redirected it to ValidatorController:checkParams with no error or warning.

Following changes I have made.
in index.php of API codebase

if (isset($_SERVER['HTTP_ORIGIN'])) { header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}"); header('Access-Control-Allow-Credentials: true'); header('Access-Control-Max-Age: 86400'); // cache for 1 day } // Access-Control headers are received during OPTIONS requests if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') { if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD'])) { header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS"); } if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS'])) { header("Access-Control-Allow-Headers: {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}"); } }

my redirection code in ProcessController which should redirect to ValidatorController:checkParams
$qryParams = http_build_query(['requestlogID'=>$result]); $uri = $request->getUri()->withQuery($qryParams)->withPath($this->_router->pathFor('checkParams')); return $response->withStatus(200) ->withHeader("Access-Control-Allow-Origin", $_SERVER['HTTP_ORIGIN']) ->withHeader("Access-Control-Allow-Credentials", true) ->withHeader("Access-Control-Max-Age", 86400) ->withHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS") ->withHeader("Location", $uri);

can you please guide what am I doing wrong?
Thanks

@niranjanwnmt
Read this: Setting up CORS

Redirecting not working...Someone please help debug
HomeController has--->
$app->get('/', 'HomeController:index')->setName('home');
AuthController has --->
return $response->withRedirect($this->router->pathFor('home'));

Not working...keeps redirecting back to auth/signup instead of index page

@bbmattieu9 please provide code in a single index.php file along with curl examples that show that it doesn't work.

index.php.zip

Attached here-with is an index file containing my source code. i hope this will help you debug the code. thanks in advance

@akrabat Attached here-with is an index file containing my source code. i hope this will help you debug the code. thanks in advance
index.php.zip

Was this page helpful?
0 / 5 - 0 ratings

Related issues

alexweissman picture alexweissman  路  38Comments

derekjones picture derekjones  路  19Comments

cacharrin picture cacharrin  路  23Comments

l0gicgate picture l0gicgate  路  31Comments

l0gicgate picture l0gicgate  路  83Comments