diff --git a/src/Illuminate/Foundation/Configuration/ApplicationBuilder.php b/src/Illuminate/Foundation/Configuration/ApplicationBuilder.php index 89ec3b9cc50f..dde1af231d43 100644 --- a/src/Illuminate/Foundation/Configuration/ApplicationBuilder.php +++ b/src/Illuminate/Foundation/Configuration/ApplicationBuilder.php @@ -269,6 +269,18 @@ public function withMiddleware(?callable $callback = null) if ($priorities = $middleware->getMiddlewarePriority()) { $kernel->setMiddlewarePriority($priorities); } + + if ($priorityAppends = $middleware->getMiddlewarePriorityAppends()) { + foreach ($priorityAppends as $middleware => $after) { + $kernel->addToMiddlewarePriorityAfter($after, $middleware); + } + } + + if ($priorityPrepends = $middleware->getMiddlewarePriorityPrepends()) { + foreach ($priorityPrepends as $middleware => $before) { + $kernel->addToMiddlewarePriorityBefore($before, $middleware); + } + } }); return $this; diff --git a/src/Illuminate/Foundation/Configuration/Middleware.php b/src/Illuminate/Foundation/Configuration/Middleware.php index 52b83acf518b..52cf908d61d9 100644 --- a/src/Illuminate/Foundation/Configuration/Middleware.php +++ b/src/Illuminate/Foundation/Configuration/Middleware.php @@ -145,6 +145,20 @@ class Middleware */ protected $priority = []; + /** + * The middleware to prepend to the middleware priority definition. + * + * @var array + */ + protected $prependPriority = []; + + /** + * The middleware to append to the middleware priority definition. + * + * @var array + */ + protected $appendPriority = []; + /** * Prepend middleware to the application's global middleware stack. * @@ -400,6 +414,34 @@ public function priority(array $priority) return $this; } + /** + * Prepend middleware to the priority middleware. + * + * @param array|string $before + * @param string $prepend + * @return $this + */ + public function prependToPriorityList($before, $prepend) + { + $this->prependPriority[$prepend] = $before; + + return $this; + } + + /** + * Append middleware to the priority middleware. + * + * @param array|string $after + * @param string $append + * @return $this + */ + public function appendToPriorityList($after, $append) + { + $this->appendPriority[$append] = $after; + + return $this; + } + /** * Get the global middleware. * @@ -766,4 +808,24 @@ public function getMiddlewarePriority() { return $this->priority; } + + /** + * Get the middleware to prepend to the middleware priority definition. + * + * @return array + */ + public function getMiddlewarePriorityPrepends() + { + return $this->prependPriority; + } + + /** + * Get the middleware to append to the middleware priority definition. + * + * @return array + */ + public function getMiddlewarePriorityAppends() + { + return $this->appendPriority; + } }