2016-11-17 89 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