php 如何使用纤薄重定向
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23404355/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to use slim redirect
提问by dKab
I have a problem. I am using slim and I have route for my main page:
我有个问题。我正在使用 slim 并且我的主页有路线:
$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/
但是我没有重定向到“/”路由,而是重定向到本地服务器的根目录 http://localhost/
What am I doing wrong? How should I use redirect method?
我究竟做错了什么?我应该如何使用重定向方法?
回答by Adrian Wragg
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:
Slim 允许您命名路由,然后使用urlFor()
. 在您的示例中,将您的路线更改为:
$app->get('/', function() use ($app) { ... })->name("root");
and then your redirection becomes:
然后你的重定向变成:
$app->response->redirect($app->urlFor('root'), 303);
See Route Helpersin the Slim documentation for more information.
有关更多信息,请参阅Slim 文档中的路由助手。
回答by Gene Bo
From Slim3 docs http://www.slimframework.com/docs/start/upgrade.html
来自 Slim3 文档 http://www.slimframework.com/docs/start/upgrade.html
$app->get('/', function ($req, $res, $args) {
return $res->withStatus(302)->withHeader('Location', 'your-new-uri');
});
回答by Arthur Araújo
Slim 3
修身 3
$app->get('/', function ($req, $res, $args) {
$url = 'https://example.org';
return $res->withRedirect($url);
});
Reference: https://www.slimframework.com/docs/v3/objects/response.html#returning-a-redirect
参考:https: //www.slimframework.com/docs/v3/objects/response.html#returning-a-redirect
回答by Christopher Chase
give your '/' route a name,
给你的“/”路线一个名字,
$app = new \Slim\Slim();
$app->get('/', function () {
echo "root page";
})->name('root');
$app->get('/foo', function () use ($app) {
$app->redirect($app->urlFor('root') );
});
$app->run();
This should give you the correct url to redirect
这应该为您提供正确的重定向网址
http://docs.slimframework.com/routing/names/http://docs.slimframework.com/routing/helpers/#redirect
http://docs.slimframework.com/routing/names/ http://docs.slimframework.com/routing/helpers/#redirect
回答by MNR
For Slim v3.x:
对于 Slim v3.x:
Use $response->withStatus(302)->withHeader('Location', $url);
instead of $app->redirect();
使用$response->withStatus(302)->withHeader('Location', $url);
代替$app->redirect();
In Slim v2.x one would use the helper function $app->redirect(); to trigger a redirect request. In Slim v3.x one can do the same with using the Response class like so (see the following example)[1].
在 Slim v2.x 中,可以使用辅助函数 $app->redirect(); 触发重定向请求。在 Slim v3.x 中,可以像这样使用 Response 类来做同样的事情(见下面的例子)[1]。
Use pathFor()
instead of urlFor()
:
使用pathFor()
代替urlFor()
:
urlFor() has been renamed pathFor() and can be found in the router object. Also, pathFor() is base path aware[2].
urlFor() 已重命名为 pathFor() 并且可以在路由器对象中找到。此外, pathFor() 是基本路径感知 [2]。
Example:
例子:
$app->get('/', function ( $request, $response, $args ) use ( $app ) {
$url = $this->router->pathFor('loginRoute');
return $response->withStatus(302)->withHeader('Location', $url);
});
Note:additional parameters can be supplied by passing an associative array of parameter names and values as a second argument of pathFor()
like: $this->router->pathFor('viewPost', ['id' => 1]);
.
注意:可以通过传递参数名称和值的关联数组作为pathFor()
like: 的第二个参数来提供附加参数$this->router->pathFor('viewPost', ['id' => 1]);
。
The router's pathFor() method accepts two arguments:
- The route name
- Associative array of route pattern placeholders and replacement values[3]
路由器的 pathFor() 方法接受两个参数:
- 路线名称
- 路由模式占位符和替换值的关联数组 [3]
References:
参考:
回答by bamossza
//Set Default index or home page
$app->get('/', function() use ($app) {
$app->response->redirect('login.php');
});
回答by Wiston Coronell
I think I faced a similar problem, and the issue was with my .htaccessconfig file. It should actually be something like this:
我想我遇到了类似的问题,问题出在我的.htaccess配置文件上。它实际上应该是这样的:
RewriteEngine On
RewriteBase /api #if your web service is inside a subfolder of your app,
# you need to pre-append the relative path to that folder, hope this helps you!
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]
回答by HardlyNoticeable
I think using ./ instead of / will work also.
我认为使用 ./ 而不是 / 也可以。