对象在内部返回一个额外的 NULL 数组

Object returning an extra NULL array inside

所以我用 Slim 框架构建了一个简单的 API,同时使用一个中间件从这里 https://github.com/Respect/Validation 通过一个函数进行验证,如本指南所述:

https://www.codecourse.com/lessons/slim-3-authentication/769
https://www.codecourse.com/lessons/slim-3-authentication/770

除了一件特定的事情外,一切似乎都很好:

通过本指南,我应该能够验证我的表单及其输入,然后在我的 Twig 页面上打印这些错误,例如通过这样做 {{ errors | json_encode }}(用于测试),问题是即使验证函数本身可以正常工作我似乎无法得到错误,因为它们总是 return null。

通过使用 var_dump 我注意到那里有 "errors",但格式似乎与指南视频中显示的格式有点不同,因为我的 var_dump 在那里似乎是一个额外的阵列? (不确定)显示 NULL,并且仅在此之后还有其他数组包含这些错误,所以我认为这就是它不起作用的原因?

这是我在 var_dump 时得到的示例:

object(App\Validation\Validator)#79 (2) { ["errors":protected]=> NULL [""]=> array(1) { ["uniquevisitors"]=> array(2) { [0]=> string(30) "Uniquevisitors must be numeric" [1]=> string(31) "Uniquevisitors must be positive" } } }

那是我 var_dump 它与 get_object_vars:

array(1) { [""]=> array(1) { ["uniquevisitors"]=> array(2) { [0]=> string(30) "Uniquevisitors must be numeric" [1]=> string(31) "Uniquevisitors must be positive" } } }

我认为 { ["errors":protected]=> NULL [""]=> 不应该在那里。 我的代码与该指南中的代码完全相同,但它为他产生的结果略有不同。关于 'uniquevisitors' 的错误是应该的,但我认为 null 不应该在那里,我认为这可能是我的 Twig 模板无法读取它并始终显示 null 的原因,但我可能错了。

在该中间件中使用全局 and/or 会话会不会有问题? JFYI 我也使用其他中间件,但自从我添加它以来一直是这种方式,以防万一我在另一个 "clean" slim 上再次 运行 它并获得相同的输出。当我做一些研究时,我发现一个案例有完全相同的问题但没有答案所以我相信代码可能有问题?或者可能与我的环境有关?我在本地 运行 使用 xampp.


middleware.php

<?php

namespace App\Middleware;

class Middleware {
    protected $container;

    public function __construct($container) {
        $this->container = $container;
    }
}

ValidationErrorsMiddleware.php

<?php

namespace App\Middleware;

class ValidationErrorsMiddleware extends Middleware {
    protected $container;

    public function __invoke($request, $response, $next) {
        if (isset($_SESSION['errors'])) {
        $this->container->view->getEnvironment()->addGlobal('errors', $_SESSION['errors']);
        unset($_SESSION['errors']);
    }
        $response = $next($request, $response);
        return $response;
    }
    }

Validator.php

<?php

namespace App\Validation;

use Respect\Validation\Validator as Respect;
use Respect\Validation\Exceptions\NestedValidationException;

class Validator {

protected $errors;

    public function validate($request, array $rules) {

        foreach($rules as $field => $rule) {
            try {
                $rule->setName(ucfirst($field))->assert($request->getParam($field));
            } catch (NestedValidationException $e) {
                $errors='';
                $this->$errors[$field] = $e->getMessages();
            }
        }
        $_SESSION['errors'] = $this->errors;
        return $this;
    }
    public function failed(){
        $errors='';
        return !empty($this->$errors);
    }
}

出于某种原因,当我想通过使用 {{ errors.field }} 在 Twig 表单中分别 post 这些错误时,它保持空白或为空,(与 errors | json_encode 相同,它保持空).验证本身有效,并且 var_dump 显示存在这些错误,但出于某种原因我无法将它们 posted on twig,我认为这是因为我的 'errors' 包含一些额外的 Null那不应该在那里。

我使用的代码与我在第一个 post 中提到的那个指南中 post 编辑的代码相同,它似乎对这个人有效,但对我无效。

你的验证器有一些问题,此时你正试图将错误分配给动态 属性$errors,

$errors = '';
$this->$errors[$field] = $e->getMessages();

大致 "translated" 这意味着,将 $e->getMessages() 的输出添加到 属性 $this->{empty string} 因为 $errors 定义为 ""这就是 "weird" 输出的原因 var_dump

你真正要找的是这个,

$this->errors[$field] = $e->getMessages();

你的 validate 函数也是如此

public function failed(){
    return !empty($this->errors); //and not $this->$errors
}