【发布时间】:2019-02-22 22:14:20
【问题描述】:
我有一个 Middleware 目录包含 3 个类,即 OldInputMiddleware、MiddleWare 和 ErrorsMiddleware:
class Middleware {
protected $container;
public function __construct($container){
$this->container = $container;
}
class OldInputMiddleware extends Middleware {
public function __invoke($request, $response, $next){
if (isset($_SESSION['errors'])) {
$this->container->view->getEnvironment()->addGlobal('old', $_SESSION['old']);
$_SESSION['old'] = $request->getParams();
}
//always return response with the next occurring
// request and response
$response = $next($request, $response);
return $response;
}
ErrorsMiddleware 与我在 OldInputMiddleware 中的类似,但它在检索后取消设置会话变量,并且在我的 signup.twig 文件中:
<div class="form-group {{ errors.email ? 'has-error' : '' }}">
<label for="email">Email</label>
<input type="text" name="email" id="email" placeholder="someone@domain.com" class="form-control" value="{{ old.email }}">
{% if errors.email %}
<span class="help-block">{{ errors.email | first }}</span>
{% endif %}
ErrorsMiddleware 工作正常并且确实返回了相关的错误消息,但是当我尝试通过在 value 属性中添加树枝注释来保留旧数据时,数据没有被检索到。
【问题讨论】: