PHP Slim - 重定向无效
PHP Slim - Redirect not working
我知道这个问题已经有了答案,但是 none 对我有用。出于某种原因我不能使用:
$app->redirect($app->urlFor('home'));
或
$app->response->redirect($app->urlFor('home'));
路由 'home' 已定义,但出于某种原因,Slim 只是 return 发送一个空的 200 响应。我做错了什么吗?
编辑:
完整代码:
$app->get('/gensession', function() use ($app){
// set up session; this works
// if I echo anything here, it'll output on the page
$app->redirect('/'); // This doesn't work, neither does urlFor(<name>)
return;
});
这只会让我留在 /gensession 的空白页上。 Slim returns 200(OK) 没有别的。没有错误。无输出。
.htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]
编辑 2:
$app->response 的输出是:
object(Slim\Http\Response)#113 (5) { ["status":protected]=> int(302) ["headers"]=> object(Slim\Http\Headers)#115 (1) { ["data":protected]=> array(2) { ["Content-Type"]=> string(9) "text/html" ["Location"]=> string(1) "/" } } ["cookies"]=> object(Slim\Http\Cookies)#116 (2) { ["defaults":protected]=> array(6) { ["value"]=> string(0) "" ["domain"]=> NULL ["path"]=> NULL ["expires"]=> NULL ["secure"]=> bool(false) ["httponly"]=> bool(false) } ["data":protected]=> array(0) { } } ["body":protected]=> string(0) "" ["length":protected]=> int(0) }
注意:["status":protected]=> int(302)
和 ["Location"]=> string(1) "/"
那么,如果响应对象明确包含这些属性,为什么不 return 将它们发送给客户端?
您应该 return 重定向如下:
$app->get('/gensession', function() use ($app){
return $app->redirect('/');
});
实际答案:
No idea what I did other than change the session save path, but it now works. I decided to just accept the only answer as it could be helpful for others later. Thank you very much for all your suggestions. – Crazy Redd
问题可能出在中间件上。
我使用的是 slim-minify(中间件),显然没有正确编写(据我所知)--
原始代码(片段):
public function __invoke(Request $request, Response $response,callable $next)
{
$next($request,$response);
$oldBody = $response->getBody();
$minifiedBodyContent = $this->minifyHTML((string)$oldBody);
$newBody = new Body(fopen('php://temp', 'r+'));
//write the minified html content to the new \Slim\Http\Body instance
$newBody->write($minifiedBodyContent);
return $response->withBody($newBody);
}
注意 $next($request,$response)
的用法。在 Slim 中,$response 是不可变的(你不能修改变量)所以你必须用 $next()
函数的 return 值覆盖变量。
$response = $next($request,$response);
没有
$next($request,$response);
我知道这个问题已经有了答案,但是 none 对我有用。出于某种原因我不能使用:
$app->redirect($app->urlFor('home'));
或
$app->response->redirect($app->urlFor('home'));
路由 'home' 已定义,但出于某种原因,Slim 只是 return 发送一个空的 200 响应。我做错了什么吗?
编辑: 完整代码:
$app->get('/gensession', function() use ($app){
// set up session; this works
// if I echo anything here, it'll output on the page
$app->redirect('/'); // This doesn't work, neither does urlFor(<name>)
return;
});
这只会让我留在 /gensession 的空白页上。 Slim returns 200(OK) 没有别的。没有错误。无输出。
.htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]
编辑 2: $app->response 的输出是:
object(Slim\Http\Response)#113 (5) { ["status":protected]=> int(302) ["headers"]=> object(Slim\Http\Headers)#115 (1) { ["data":protected]=> array(2) { ["Content-Type"]=> string(9) "text/html" ["Location"]=> string(1) "/" } } ["cookies"]=> object(Slim\Http\Cookies)#116 (2) { ["defaults":protected]=> array(6) { ["value"]=> string(0) "" ["domain"]=> NULL ["path"]=> NULL ["expires"]=> NULL ["secure"]=> bool(false) ["httponly"]=> bool(false) } ["data":protected]=> array(0) { } } ["body":protected]=> string(0) "" ["length":protected]=> int(0) }
注意:["status":protected]=> int(302)
和 ["Location"]=> string(1) "/"
那么,如果响应对象明确包含这些属性,为什么不 return 将它们发送给客户端?
您应该 return 重定向如下:
$app->get('/gensession', function() use ($app){
return $app->redirect('/');
});
实际答案:
No idea what I did other than change the session save path, but it now works. I decided to just accept the only answer as it could be helpful for others later. Thank you very much for all your suggestions. – Crazy Redd
问题可能出在中间件上。
我使用的是 slim-minify(中间件),显然没有正确编写(据我所知)--
原始代码(片段):
public function __invoke(Request $request, Response $response,callable $next)
{
$next($request,$response);
$oldBody = $response->getBody();
$minifiedBodyContent = $this->minifyHTML((string)$oldBody);
$newBody = new Body(fopen('php://temp', 'r+'));
//write the minified html content to the new \Slim\Http\Body instance
$newBody->write($minifiedBodyContent);
return $response->withBody($newBody);
}
注意 $next($request,$response)
的用法。在 Slim 中,$response 是不可变的(你不能修改变量)所以你必须用 $next()
函数的 return 值覆盖变量。
$response = $next($request,$response);
没有
$next($request,$response);