Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Alter routes options and requirements
<?php
namespace Drupal\my_module\Routing;
use Drupal\Core\Routing\RouteSubscriberBase;
use Symfony\Component\Routing\RouteCollection;
/**
* Listens to the dynamic route events.
*/
class JwtRouteSubscriber extends RouteSubscriberBase {
/**
* Alters existing routes for a specific collection.
*
* @param \Symfony\Component\Routing\RouteCollection $collection
* The route collection for adding routes.
*/
public function alterRoutes(RouteCollection $collection) {
$urls = [
'view.xxx.rest_artworks',
'view.uuu.rest_export_actives',
];
foreach ($urls as $url) {
// Load the route, set authentication and add it again.
$route = $collection->get($url);
$route->setOption('_auth', ['jwt_auth', 'basic_auth', 'cookie']);
$route->setRequirement('_access', 'TRUE');
$collection->add($url, $route);
}
}
}
services:
my_module.route_subscriber:
class: Drupal\my_module\Routing\JwtRouteSubscriber
tags:
- { name: event_subscriber }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment