ShareErrorsFromSession.php 1.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. namespace Illuminate\View\Middleware;
  3. use Closure;
  4. use Illuminate\Support\ViewErrorBag;
  5. use Illuminate\Contracts\View\Factory as ViewFactory;
  6. class ShareErrorsFromSession
  7. {
  8. /**
  9. * The view factory implementation.
  10. *
  11. * @var \Illuminate\Contracts\View\Factory
  12. */
  13. protected $view;
  14. /**
  15. * Create a new error binder instance.
  16. *
  17. * @param \Illuminate\Contracts\View\Factory $view
  18. * @return void
  19. */
  20. public function __construct(ViewFactory $view)
  21. {
  22. $this->view = $view;
  23. }
  24. /**
  25. * Handle an incoming request.
  26. *
  27. * @param \Illuminate\Http\Request $request
  28. * @param \Closure $next
  29. * @return mixed
  30. */
  31. public function handle($request, Closure $next)
  32. {
  33. // If the current session has an "errors" variable bound to it, we will share
  34. // its value with all view instances so the views can easily access errors
  35. // without having to bind. An empty bag is set when there aren't errors.
  36. $this->view->share(
  37. 'errors', $request->session()->get('errors') ?: new ViewErrorBag
  38. );
  39. // Putting the errors in the view for every view allows the developer to just
  40. // assume that some errors are always available, which is convenient since
  41. // they don't have to continually run checks for the presence of errors.
  42. return $next($request);
  43. }
  44. }