protected function addMiddlewareToRoute($name, array $middleware)
{
// Get all registered app routes
$routes = $this->getRoutes()->getIterator();
// Check if the named route is a wildcard route
$wildcard = false;
if (ends_with($name, '*')) {
$wildcard = true;
$name = substr($name, 0, -1);
}
/** @var \Illuminate\Routing\Route $route */
foreach ($routes as $route) {
// Only apply middleware if route name matches 1-to-1 and it's not a wildcard
// or if it is a wildcard and the first part before the asterisk is the beginning
// of the route's name.
if (!$wildcard && $route->getName() !== $name || $wildcard && !starts_with($route->getName(), $name)) {
continue;
}
// If we have any middleware registered to the route, apply it
$route->middleware($middleware);
}
}