Quantcast
Channel: Issues for Drupal core
Viewing all articles
Browse latest Browse all 292750

Dynamic routes via service tags. Possible deprecation of route_callbacks

$
0
0

Problem/Motivation

Modules provding only 'dynamic' routes are forced to create a redundant .routing.yml that serves no purpose other than to tell Drupal core the location/class that provides the routes.

//@file module.routing.yml
route_callbacks:
  - Drupal\module\Routes\RouteDefiner::getRoutes

From my perspective (may not be shared, granted) i tend to try and avoid hard typed strings, thus, for route id's, permission key's etc i tend to use public class constants in classes that are similar to KernelEvents etc.

Using class constants (whilst not impossible) is difficult in .yml definition files and also causes problems when defining a route with access checks on multiple permissions where one is a class constant.

//@file module.routing.yml
module.route:
  path: '/example-route'
  defaults:
    ...
  requirements:
    _permission: !php/const Drupal\module\Permissions::A_PERMISSION,access content

The above will throw a yaml parse exception even after yaml parses have been patched (https://www.drupal.org/project/drupal/issues/2951046) ending the ability to use constants when trying to defined multiple permissions against a route.

Because of the above problems, and of course my approach to development, i am forced down the 'dynamic' (quoted) as these routes are infact static and i am forced to create alot (what i would deem) unnecessary .routing.yml files that do nothing more than provide the 'route_callbacks'.

These 2 line .routing.yml files could easiliy be replaced with a tagged service.

Proposed resolution

  • Create a Drupal\Core\Routing\RouteDefinerInterface
    • Create a base implementation of this interface, making it easier for modules to define routes via a 'definer'
  • Configure Drupal\Core\Routing\RouteBuilder as a service collector
    • Introduce the service tag route_definer which Drupal\Core\Routing\RouteBuilder will collect
    • All services tagged with route_definerMUST implement Drupal\Core\Routing\RouteDefinerInterface
    • On route build, extract routes (RouteCollection) objects from collected definers injecting them into the master collection

The above would allow the following

//@file module.routing.yml
route_callbacks:
  - Drupal\module\Routes\RouteDefiner::getRoutes

to become

//@file module.services.yml
services:
  module.route_definer:
    class: Drupal\module\RouteDefiner
    tags: 
      - { name: route_definer }

In the above simple example, this may seem like more lines in a file, but imagine a module implementing alot of services, and only a single route callback.

With this could come the possible deprecation of route_callbacks in .routing.yml
However providing the ability for module developers to provided both static and actually dynamic routes via routing.yml is not a bad thing.

Remaining tasks

  • Reviews of the provided patch
  • Tests written (apologies, not my strong point here..)
  • Inclusion of the tagged services method in routing documentation

It would be great to hear everyones feedback on this, im open to all input.

Thanks,
Josh


Viewing all articles
Browse latest Browse all 292750

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>