$npx -y skills add edutrul/drupal-ai --skill drupal-routesDrupal routing — routing.yml, admin routes, settings form routes, controllers, route parameters, title callbacks, entity upcasting, permissions, and URL/link generation.
| 1 | # Drupal Routes |
| 2 | |
| 3 | ## routing.yml |
| 4 | |
| 5 | ```yaml |
| 6 | # my_module.routing.yml |
| 7 | |
| 8 | my_module.page: |
| 9 | path: '/my-page' |
| 10 | defaults: |
| 11 | _controller: '\Drupal\my_module\Controller\MyController::content' |
| 12 | _title: 'My Page' |
| 13 | requirements: |
| 14 | _permission: 'access content' |
| 15 | |
| 16 | # Route with parameter |
| 17 | my_module.entity_page: |
| 18 | path: '/items/{item_id}' |
| 19 | defaults: |
| 20 | _controller: '\Drupal\my_module\Controller\ItemController::view' |
| 21 | _title_callback: '\Drupal\my_module\Controller\ItemController::title' |
| 22 | requirements: |
| 23 | _permission: 'access content' |
| 24 | item_id: '\d+' |
| 25 | |
| 26 | # Admin route |
| 27 | my_module.admin: |
| 28 | path: '/admin/config/my-module' |
| 29 | defaults: |
| 30 | _form: '\Drupal\my_module\Form\SettingsForm' |
| 31 | _title: 'My Module Settings' |
| 32 | requirements: |
| 33 | _permission: 'administer my module' |
| 34 | |
| 35 | # Route with entity upcasting |
| 36 | my_module.node_page: |
| 37 | path: '/custom/{node}' |
| 38 | defaults: |
| 39 | _controller: '\Drupal\my_module\Controller\NodeController::view' |
| 40 | _title: 'Node Custom Page' |
| 41 | requirements: |
| 42 | _entity_access: 'node.view' |
| 43 | options: |
| 44 | parameters: |
| 45 | node: |
| 46 | type: entity:node |
| 47 | ``` |
| 48 | |
| 49 | ## Controller |
| 50 | |
| 51 | ```php |
| 52 | // src/Controller/MyController.php |
| 53 | namespace Drupal\my_module\Controller; |
| 54 | |
| 55 | use Drupal\Core\Controller\ControllerBase; |
| 56 | |
| 57 | final class MyController extends ControllerBase { |
| 58 | |
| 59 | public function content(): array { |
| 60 | return [ |
| 61 | '#markup' => $this->t('Hello world'), |
| 62 | '#cache' => [ |
| 63 | 'contexts' => ['user.permissions'], |
| 64 | ], |
| 65 | ]; |
| 66 | } |
| 67 | |
| 68 | public function view(int $item_id): array { |
| 69 | // $item_id is from route parameter |
| 70 | return ['#markup' => $this->t('Item @id', ['@id' => $item_id])]; |
| 71 | } |
| 72 | |
| 73 | public function title(int $item_id): string { |
| 74 | return $this->t('Item @id', ['@id' => $item_id]); |
| 75 | } |
| 76 | |
| 77 | } |
| 78 | ``` |
| 79 | |
| 80 | ## Generating URLs and Links |
| 81 | |
| 82 | ```php |
| 83 | use Drupal\Core\Url; |
| 84 | use Drupal\Core\Link; |
| 85 | |
| 86 | // URL from route |
| 87 | $url = Url::fromRoute('my_module.page'); |
| 88 | $url = Url::fromRoute('my_module.entity_page', ['item_id' => 42]); |
| 89 | $url = Url::fromRoute('entity.node.canonical', ['node' => $nid]); |
| 90 | |
| 91 | // Absolute URL |
| 92 | $url = Url::fromRoute('my_module.page', [], ['absolute' => TRUE]); |
| 93 | |
| 94 | // URL from path |
| 95 | $url = Url::fromUserInput('/my-page'); |
| 96 | |
| 97 | // Link |
| 98 | $link = Link::fromTextAndUrl($this->t('My Link'), $url); |
| 99 | $render = $link->toRenderable(); |
| 100 | ``` |