Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
722 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...