Slim phpフレームワークを初めて使用しています。これまでのところ優れています。私はユーザー登録フォームと入力を格納し、必要なときにエラーをスローするセッション変数の検証を処理するライブラリを使用しています。セッション変数からSlim throwing未定義インデックス
セッション変数が定義されていないというエラーが表示されます。
Validator.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) {
$this->errors[$field] = $e->getMessages();
}
}
$_SESSION['errors'] = $this->errors;
return $this;
}
public function failed()
{
return !empty($this->errors);
}
}
ValidationErrorsMiddleware.php
namespace App\Middleware;
class ValidationErrorsMiddleware extends Middleware
{
public function __invoke($request, $response, $next)
{
$this->container->view->getEnvironment()->addGlobal('errors', $_SESSION['errors']);
unset($_SESSION['errors']);
$response = $next($request, $response);
return $response;
}
}
エラーが投げ続けた:私は、私は失敗していた場合、セッション開始がapp.php
で始まり、次のファイルが存在して見るのに苦労していますこの行を指す:
$this->container->view->getEnvironment()->addGlobal('errors', $_SESSION['errors']);
essionはログに記録されていますが、これにはisset
を設定しないと、なぜ失敗するのか知りたいですか?
実際のエラー自体は次のとおりです。Notice: Undefined index: errors in...
UPDATE
app.php
$app->add(new \App\Middleware\ValidationErrorsMiddleware($container));
Middleware.php
namespace App\Middleware;
class Middleware
{
protected $container;
public function __construct($container)
{
$this->container = $container;
}
}
を?検証機能が実行されていますか? – puelo
更新 – danjbh
を参照してください。私はミドルウェアではなく、実際のValidatorクラスを意味しました。たぶん私は盲目ですが、クラスがあなたの流れにどのように統合されているか分かりません。 – puelo