Skip to content

Instantly share code, notes, and snippets.

@keopx
Created December 2, 2017 09:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save keopx/fc84a88d4946ce399af276ef33666dd2 to your computer and use it in GitHub Desktop.
Save keopx/fc84a88d4946ce399af276ef33666dd2 to your computer and use it in GitHub Desktop.
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