Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Alter routes options and requirements
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 = [
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);
class: Drupal\my_module\Routing\JwtRouteSubscriber
- { name: event_subscriber }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment