2016-11-17 142 views
0

後請求存儲數據我想將用戶重定向到有錯誤的登錄頁面和提示信息。如何重定向和重定向

目前我在做這個:

return $this->container->view->render($response,'admin/partials/login.twig',['errorss'=>$errors]); 

但我想重定向到登錄頁面,同時還具有errror消息和提示信息。我想這樣,但不工作:

$this->container->flash->addMessage('fail',"Please preview the errors and login again."); 
return $response->withRedirect($this->container->router->pathFor('admin.login',['errors'=>$errors])); 

回答

1

你已經使用slim/flash,但你這樣做:

return $response->withRedirect($this->container->router->pathFor('admin.login',['errors'=>$errors])); 

這是不正確的。在Router#pathFor()方法的第二個參數是不用於數據到重定向之後使用

路由器的pathFor()方法接受兩個參數:

  1. 路線名稱
  2. 路線模式的佔位符的
  3. 關聯數組和替換值

源(http://www.slimframework.com/docs/objects/router.html

所以,你可以設置佔位符像profile/{name}與第二個參數。

現在你需要將所有加在一起你的錯誤,到slim/flash`。

我在修改Usage Guide of slim/flash

// can be 'get', 'post' or any other method 
$app->get('/foo', function ($req, $res, $args) { 
    // do something to get errors 
    $errors = ['first error', 'second error']; 

    // store messages for next request 
    foreach($errors as $error) { 
     $this->flash->addMessage('error', $error); 
    } 

    // Redirect 
    return $res->withStatus(302)->withHeader('Location', $this->router->pathFor('bar')); 
}); 

$app->get('/bar', function ($request, $response, $args) { 
    // Get flash messages from previous request 
    $errors = $this->flash->getMessage('error'); 

    // $errors is now ['first error', 'second error'] 

    // render view 
    $this->view->render($response, 'admin/partials/login.twig', ['errors' => $errors]); 
})->setName('bar'); 
+0

expaining此謝謝@jmattheis,我也看看修身文檔,但無法理解,因爲我是掃描重定向包括數據(知道我應該避免只掃描)。但現在我明白了它的工作方式..再次感謝:) .. – ryan