在 Twig 中访问 Slim Flash 数据
Access Slim Flash Data in Twig
如何在我的 Twig 视图中访问我在 Slim
路由中设置的即时消息?这是我的路线:
$app->post('/register', function () use ($app) {
$v = new \Valitron\Validator($_POST);
$v->rule('required', [
'username',
'password',
'password_confirm',
'firstname',
'surname',
'email',
'paypal']
)->message('{field} is required');
$v->rule('email', ['email', 'paypal'])->message('{field} is not a valid email');
$v->rule('alpha', ['firstname', 'surname'])->message('{field} can only contain alpha characters');
$v->rule('alphaNum', ['username'])->message('{field} can only contain alpha characters and numbers');
$v->rule('equals', 'password', 'password_confirm')->message('Passwords do not match');
if($v->validate() === false) {
# How do I access this from within a view
$app->flash('errors', $v->errors());
$app->redirect('/register');
}
# More code ...
});
我在 index.php
的开头使用了 session_start()
。我只是不明白如何访问即显消息。
假设数组 $v->errors()
看起来像这样:
array(1) {
["email"]=> string(34) "Email is not a valid email address"
}
您可以在您的视图中使用它 {{flash.errors}}
并检索电子邮件:{{flash.errors.email}}
如何在我的 Twig 视图中访问我在 Slim
路由中设置的即时消息?这是我的路线:
$app->post('/register', function () use ($app) {
$v = new \Valitron\Validator($_POST);
$v->rule('required', [
'username',
'password',
'password_confirm',
'firstname',
'surname',
'email',
'paypal']
)->message('{field} is required');
$v->rule('email', ['email', 'paypal'])->message('{field} is not a valid email');
$v->rule('alpha', ['firstname', 'surname'])->message('{field} can only contain alpha characters');
$v->rule('alphaNum', ['username'])->message('{field} can only contain alpha characters and numbers');
$v->rule('equals', 'password', 'password_confirm')->message('Passwords do not match');
if($v->validate() === false) {
# How do I access this from within a view
$app->flash('errors', $v->errors());
$app->redirect('/register');
}
# More code ...
});
我在 index.php
的开头使用了 session_start()
。我只是不明白如何访问即显消息。
假设数组 $v->errors()
看起来像这样:
array(1) {
["email"]=> string(34) "Email is not a valid email address"
}
您可以在您的视图中使用它 {{flash.errors}}
并检索电子邮件:{{flash.errors.email}}