How to redirect to different route in slim 3?
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.
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.
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
Most helpful comment
or: