无法从中间件Slim检索旧数据

问题描述 投票:0回答:2

我有一个中间件目录包含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="[email protected]" class="form-control" value="{{ old.email }}">
{% if errors.email %}
    <span class="help-block">{{ errors.email | first }}</span>
{% endif %}

错误中间件工作正常并返回相关的错误消息,但是当我尝试通过在value属性中添加twig注释来保留旧数据时,数据未被检索。

html twig slim
2个回答
0
投票

回答我自己,这句话是一个愚蠢的错误:

if (isset($_SESSION['errors']))

应该是$ _SESSION ['old']而不是$ _SESSION ['errors']


0
投票

您可以删除if语句。没有必要。

您可以在没有if语句的情况下编写如下。

$this->container->view->getEnvironment()->addGlobal('old', $_SESSION['old']);
$_SESSION['old'] = $request->getParams();
© www.soinside.com 2019 - 2024. All rights reserved.