From 47be256b88ad1adc46b668c1af7393eb618357de Mon Sep 17 00:00:00 2001 From: ARCANEDEV Date: Tue, 29 Dec 2015 18:30:40 +0000 Subject: [PATCH] Fix #24 - Middleware groups for Laravel 5.2 --- src/Traits/LocalizationKernelTrait.php | 36 +++++++++++++++++++++++--- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/src/Traits/LocalizationKernelTrait.php b/src/Traits/LocalizationKernelTrait.php index b5deb30..c9cf5e0 100644 --- a/src/Traits/LocalizationKernelTrait.php +++ b/src/Traits/LocalizationKernelTrait.php @@ -8,10 +8,15 @@ * * @property \Illuminate\Foundation\Application app * @property \Arcanedev\Localization\Routing\Router router + * @property array middlewareGroups * @property array routeMiddleware */ trait LocalizationKernelTrait { + /* ------------------------------------------------------------------------------------------------ + | Main Functions + | ------------------------------------------------------------------------------------------------ + */ /** * Get the route dispatcher callback. * @@ -21,10 +26,35 @@ protected function dispatchToRouter() { $this->router = $this->app['router']; - foreach ($this->routeMiddleware as $name => $middleware) { - $this->router->middleware($name, $middleware); - } + $this->registerMiddlewareGroups(); + $this->registerMiddleware(); return parent::dispatchToRouter(); } + + /* ------------------------------------------------------------------------------------------------ + | Middleware Functions + | ------------------------------------------------------------------------------------------------ + */ + /** + * Register middleware groups to router (since Laravel 5.2) + */ + protected function registerMiddlewareGroups() + { + if (property_exists($this, 'middlewareGroups')) { + foreach ($this->middlewareGroups as $key => $middleware) { + $this->router->middlewareGroup($key, $middleware); + } + } + } + + /** + * Register middleware to router + */ + protected function registerMiddleware() + { + foreach ($this->routeMiddleware as $key => $middleware) { + $this->router->middleware($key, $middleware); + } + } }