【问题标题】:Can't retrieve old data from middleware Slim无法从中间件 Slim 中检索旧数据
【发布时间】: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 属性中添加树枝注释来保留旧数据时,数据没有被检索到。

【问题讨论】:

    标签: html twig slim


    【解决方案1】:

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

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

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

    【讨论】:

      【解决方案2】:

      你可以删除 if 语句。没必要。

      不用if语句也可以写成如下。

      $this->container->view->getEnvironment()->addGlobal('old', $_SESSION['old']);
      $_SESSION['old'] = $request->getParams();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-04-20
        • 2023-03-27
        • 1970-01-01
        • 2019-08-29
        • 2018-07-09
        相关资源
        最近更新 更多