I have a problem. I am using slim and I have route for my main page:
$app->get('/', function() use ($app) { ...
In one of my controllers I want to redirect to the main page, so I write
$app->response->redirect('/', 303);
But instead of redirection to the '/' route I'm getting redirected to the root of my local server which is http://localhost/
What am I doing wrong? How should I use redirect method?
Slim allows you to name routes, and then redirect back to them based upon this name, using urlFor()
. In your example, change your route to:
$app->get('/', function() use ($app) { ... })->name("root");
and then your redirection becomes:
$app->response->redirect($app->urlFor('root'), 303);
See Route Helpers in the Slim documentation for more information.
From Slim3 docs http://www.slimframework.com/docs/start/upgrade.html
$app->get('/', function ($req, $res, $args) { return $res->withStatus(302)->withHeader('Location', 'your-new-uri'); });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With