diff --git a/Herbert/Framework/Providers/HerbertServiceProvider.php b/Herbert/Framework/Providers/HerbertServiceProvider.php index cd07dc2..3d81931 100644 --- a/Herbert/Framework/Providers/HerbertServiceProvider.php +++ b/Herbert/Framework/Providers/HerbertServiceProvider.php @@ -105,6 +105,16 @@ public function register() 'notifier', 'Herbert\Framework\Notifier' ); + + $this->app->singleton( + 'errors', + function () + { + return session_flashed('__validation_errors', []); + } + ); + + $_GLOBALS['errors'] = $this->app['errors']; } /** diff --git a/Herbert/Framework/Providers/TwigServiceProvider.php b/Herbert/Framework/Providers/TwigServiceProvider.php index 759b0a0..80753d7 100644 --- a/Herbert/Framework/Providers/TwigServiceProvider.php +++ b/Herbert/Framework/Providers/TwigServiceProvider.php @@ -88,7 +88,7 @@ public function constructTwig() $twig->addGlobal($key, $value); } - $twig->addGlobal('errors', session()->getFlashBag()->get('__validation_errors', [])); + $twig->addGlobal('errors', $this->app['errors']); foreach ((array) $this->app['twig.functions'] as $function) { diff --git a/bootstrap/helpers.php b/bootstrap/helpers.php index 12f840d..2ea9328 100644 --- a/bootstrap/helpers.php +++ b/bootstrap/helpers.php @@ -110,6 +110,28 @@ function herbert($binding = null) } } +if ( ! function_exists('errors')) +{ + /** + * Get the errors. + * + * @param string key + * @return array + */ + function errors($key = null) + { + $errors = herbert('errors'); + $errors = isset($errors[0]) ? $errors[0] : $errors; + + if (!$key) + { + return $errors; + } + + return array_get($errors, $key); + } +} + if ( ! function_exists('session')) { /**