簡體   English   中英

PHP Slim - 重定向不起作用

[英]PHP Slim - Redirect not working

我知道這是一個已經有答案的問題,但沒有一個對我有用。 出於某種原因,我無法使用:

$app->redirect($app->urlFor('home'));

要么

$app->response->redirect($app->urlFor('home'));

路線'home'已定義,但由於某種原因,Slim只返回空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返回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) "/"

因此,如果響應對象明確包含這些屬性,為什么不將它們返回給客戶端?

問題可能是middelware。

我正在使用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()函數的返回值覆蓋變量。

$response = $next($request,$response);

$next($request,$response);

實際答案:

不知道除了改變會話保存路徑之外我做了什么,但它現在有效。 我決定接受唯一的答案,因為它可能對其他人有所幫助。 非常感謝你的所有建議。 - 瘋狂的雷德

你應該像這樣返回重定向:

$app->get('/gensession', function() use ($app){
    return $app->redirect('/');
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM