如何使用纤薄重定向 [英] how to use slim redirect

查看:28
本文介绍了如何使用纤薄重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有问题.我正在使用 slim 并且我的主页有路线:

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);

但是我没有重定向到/"路由,而是重定向到我的本地服务器的根目录,它是 http://localhost/

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 允许您命名路由,然后使用 urlFor() 根据此名称重定向回它们.在您的示例中,将您的路线更改为:

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");

然后你的重定向变成:

$app->response->redirect($app->urlFor('root'), 303);

有关详细信息,请参阅 Slim 文档中的路由助手.

See Route Helpers in the Slim documentation for more information.

这篇关于如何使用纤薄重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆