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.
In this repository119 other files it gives its agents.
- strategy-bitrix-d7-incrementalSkill
- strategy-explicit-over-magicSkill
- strategy-framework-conventionsSkill
- strategy-legacy-safe-editsSkill
- strategy-minimal-diffSkill
- strategy-no-untested-refactorSkill
- strategy-preserve-public-apiSkill
- strategy-readonly-immutabilitySkill
- strategy-security-firstSkill
- strategy-test-drivenSkill
- openapiSkill
- paginationSkill
- rest-designSkill
- versioningSkill
- cqrsSkill
- dependency-inversionSkill
- hexagonalSkill
- repository-patternSkill
- service-layerSkill
- bitrixSkill
- findings-reportSkill
- laravelSkill
- performanceSkill
- phpSkill
- project-analysisSkill
- refactoringSkill
- roi-prioritizationSkill
- securitySkill
- stack-detectionSkill
- cache-layersSkill
- component-engineSkill
- d7-eventsSkill
- d7-loaderSkill
- d7-orm-queriesSkill
- iblock-elementsSkill
- kernel-main-moduleSkill
- localizationSkill
- controllersSkill
- ormSkill
- validationSkill
- controllersSkill
- query-builderSkill
- routingSkill
- autoload-psr4Skill
- dependency-managementSkill
- scriptsSkill
- connection-poolingSkill
- mysql-indexingSkill
- redisSkill
- transactionsSkill
- ci-github-actionsSkill
- deployment-strategiesSkill
- docker-phpSkill
- env-configSkill
- associationsSkill
- entities-mappingSkill
- orm-basicsSkill
- entity-apiSkill
- module-developmentSkill
- formsSkill
- The first 60 shown.
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
- Distinctive: line 63
Do not hand-roll a controller route when JSON:API or the REST module already exposes the entity the client needs.
- Distinctive: line 64
Do not write a route and controller when editors only need a static content page that a node or Views can provide.
- Very specific: line 72
Add a route to MODULE.routing.yml with path, defaults._controller, defaults._title, and a requirements access rule.
- Very specific: line 73
Create a controller under src/Controller/ extending ControllerBase, inject services in create(), and return a render array from the method.
- Very specific: line 74
Run drush cr, then confirm an authorized user reaches the page and an unauthorized user gets 403.
- Very specific: line 78
Always set a requirements access rule (_permission, _role, _entity_access, or _custom_access) on each route.
- Distinctive: line 105
Not applicable — no I/O, auth, or untrusted input.
≈ “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.
Files
<!-- 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.ymlneeds apath, adefaults._controllerpointing at\Drupal\MODULE\Controller\Class::method, adefaults._title, and arequirementsblock.
Quick recipe
- Add a route to
MODULE.routing.ymlwithpath,defaults._controller,defaults._title, and arequirementsaccess rule. (verify: The path resolves to the controller afterdrush cr.) - Create a controller under
src/Controller/extendingControllerBase, inject services increate(), and return a render array from the method. (verify: Visiting the path renders the expected markup.) - Run
drush cr, then confirm an authorized user reaches the page and an unauthorized user gets 403. (verify: Access matches the declaredrequirements.)
Best practices
- Always set a
requirementsaccess rule (_permission,_role,_entity_access, or_custom_access) on each route; use_access: 'FALSE'for internal-only paths - Extend
ControllerBaseand, when the controller needs services, implementcreate(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-pattern | Why it fails | Do instead |
| Enforcing authorization inside the controller body | In-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 controller | Fat 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 upcasting | Manual 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
- Route silently unreachable without a requirements block — A route with no
requirementsaccess rule is treated as inaccessible and returns 403 for everyone. Fix: Add an explicit_permission,_role,_access: 'TRUE', or_custom_accessrequirement to the route. - 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
#cachetags and contexts for correct invalidation. - 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(ordrush 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
RouteMatchInterfaceandRequestparameters 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 -->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
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.

