agentleFS
AnyOrgNameAnyOrgName/php-skills/skills/drupal/routing-controllers/SKILL.md

routing-controllers

Skill4 starsChanged 4 months agoMITView on GitHub

Map paths to controller methods with a Drupal routing.yml file, return render arrays or responses from controllers, and gate access with route requirements. Use when the user is writing a Drupal controller that returns a render array or a JSON/redirect response; the user wants to define a Drupal route in routing.yml that maps a path to a controller method; the user needs to restrict a Drupal route by permission, role, or a custom access check. Keywords: drupal, routing, routing-yml, controller, access, render-array, php81.

The file

We split this skill into the rules it gives an agent

7 rules. About 0 are common, 3 are distinctive, and 4 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈3

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 4

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Do not hand-roll a controller route when JSON:API or the REST module already exposes the entity the client needs.

    line 63
  2. Distinctive:

    Do not write a route and controller when editors only need a static content page that a node or Views can provide.

    line 64
  3. Very specific:

    Add a route to MODULE.routing.yml with path, defaults._controller, defaults._title, and a requirements access rule.

    line 72
  4. Very specific:

    Create a controller under src/Controller/ extending ControllerBase, inject services in create(), and return a render array from the method.

    line 73
  5. Very specific:

    Run drush cr, then confirm an authorized user reaches the page and an unauthorized user gets 403.

    line 74
  6. Very specific:

    Always set a requirements access rule (_permission, _role, _entity_access, or _custom_access) on each route.

    line 78
  7. Distinctive:

    Not applicable — no I/O, auth, or untrusted input.

    line 105

≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md
<!-- generated: do not edit — source: knowledge/atoms/drupal/routing-controllers/ --><!-- schema: atomVersion=3 skillVersion=2 graphVersion=1 --><!-- compile: @php-skills/compiler v1.0.0 (deterministic build) -->

Drupal Routing and Controllers

When to use

  • The user is writing a Drupal controller that returns a render array or a JSON/redirect response.
  • The user wants to define a Drupal route in routing.yml that maps a path to a controller method.
  • The user needs to restrict a Drupal route by permission, role, or a custom access check.

When NOT to use

  • Do not hand-roll a controller route when JSON:API or the REST module already exposes the entity the client needs.
  • Do not write a route and controller when editors only need a static content page that a node or Views can provide.

Core guidance

  • Give each route a path, defaults._controller, and requirements — Each entry in MODULE.routing.yml needs a path, a defaults._controller pointing at \Drupal\MODULE\Controller\Class::method, a defaults._title, and a requirements block.

Quick recipe

  1. Add a route to MODULE.routing.yml with path, defaults._controller, defaults._title, and a requirements access rule. (verify: The path resolves to the controller after drush cr.)
  2. Create a controller under src/Controller/ extending ControllerBase, inject services in create(), and return a render array from the method. (verify: Visiting the path renders the expected markup.)
  3. Run drush cr, then confirm an authorized user reaches the page and an unauthorized user gets 403. (verify: Access matches the declared requirements.)

Best practices

  • Always set a requirements access rule (_permission, _role, _entity_access, or _custom_access) on each route; use _access: 'FALSE' for internal-only paths
  • Extend ControllerBase and, when the controller needs services, implement create(ContainerInterface $container) to inject them rather than using the trait shortcuts
  • Return a render array (['#markup' => ...] or ['#theme' => ...]) from controller methods so Drupal can attach cache metadata and libraries
  • Declare dynamic path segments as {node} and type-hint the controller argument (NodeInterface $node) so Drupal upcasts the raw id into the loaded entity

Anti-patterns

Anti-patternWhy it failsDo instead
Enforcing authorization inside the controller bodyIn-body checks are easy to forget or bypass and duplicate what the routing layer enforces uniformly.Declare access in the route requirements (_permission or _custom_access) so it runs before the controller.
Business logic inside the controllerFat controllers are hard to test and duplicate logic that belongs in a reusable, injectable service.Keep controllers thin: parse the request, call an injected service, and return a render array or response.
Loading the entity manually instead of upcastingManual loads skip the automatic 404 for missing entities and duplicate the upcasting Drupal already provides.Name the placeholder after the entity type ({node}) and type-hint the loaded entity in the controller.

See references/anti-patterns.md.

Common mistakes

  1. Route silently unreachable without a requirements block — A route with no requirements access rule is treated as inaccessible and returns 403 for everyone. Fix: Add an explicit _permission, _role, _access: 'TRUE', or _custom_access requirement to the route.
  2. Returning raw HTML strips cache metadata — Returning a plain string or pre-rendered HTML from a controller drops cache tags and attached libraries. Fix: Return a render array and let Drupal render it; add #cache tags and contexts for correct invalidation.
  3. New route returns 404 until the router is rebuilt — A newly added route in routing.yml returns 404 even though the YAML is correct. Fix: Run drush cr (or drush router:rebuild) after adding or editing routes.

Version notes

  • Route match and request injection stable since Drupal 8 (PHP >=8.1) — Controllers can type-hint RouteMatchInterface and Request parameters for automatic injection; this Symfony-based dispatch has been stable since Drupal 8 and continues in 10/11.

Security

  • Not applicable — no I/O, auth, or untrusted input.

Testing

  • Activation and decision behavior are verified in eval/scenarios/drupal/routing-controllers.yaml.

Additional resources

  • references/anti-patterns.md
  • references/core.md
  • references/examples.md
  • references/pitfalls.md
  • references/recipes.md
  • references/version-differences.md

Knowledge graph

Prerequisites: drupal/module-development

<!-- graph-fragment: graph/fragments/drupal/routing-controllers.yaml -->

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.