routing.api.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. /**
  3. * @file
  4. * Hooks and documentation related to the routing system.
  5. */
  6. /**
  7. * @defgroup routing Routing API
  8. * @{
  9. * Route page requests to code based on URLs.
  10. *
  11. * @section sec_overview Overview and terminology
  12. * The Drupal routing system defines how Drupal responds to URL requests that
  13. * the web server passes on to Drupal. The routing system is based on the
  14. * @link http://symfony.com Symfony framework. @endlink The central idea is
  15. * that Drupal subsystems and modules can register routes (basically, URL
  16. * paths and context); they can also register to respond dynamically to
  17. * routes, for more flexibility. When Drupal receives a URL request, it will
  18. * attempt to match the request to a registered route, and query dynamic
  19. * responders. If a match is made, Drupal will then instantiate the required
  20. * classes, gather the data, format it, and send it back to the web browser.
  21. * Otherwise, Drupal will return a 404 or 403 response.
  22. *
  23. * The following sections of this topic provide an overview of the routing API.
  24. * For more detailed information, see
  25. * https://www.drupal.org/developing/api/8/routing
  26. *
  27. * @section sec_register Registering simple routes
  28. * To register a route, add lines similar to this to a module_name.routing.yml
  29. * file in your top-level module directory:
  30. * @code
  31. * dblog.overview:
  32. * path: '/admin/reports/dblog'
  33. * defaults:
  34. * _controller: '\Drupal\dblog\Controller\DbLogController::overview'
  35. * _title: 'Recent log messages'
  36. * requirements:
  37. * _permission: 'access site reports'
  38. * @endcode
  39. * Some notes:
  40. * - The first line is the machine name of the route. Typically, it is prefixed
  41. * by the machine name of the module that defines the route, or the name of
  42. * a subsystem.
  43. * - The 'path' line gives the URL path of the route (relative to the site's
  44. * base URL). Generally, paths in Drupal are treated as case-insensitive,
  45. * which overrides the default Symfony behavior. Specifically:
  46. * - If different routes are defined for /example and /EXAmplE, the exact
  47. * match is respected.
  48. * - If there is no exact match, the route falls back to a case-insensitive
  49. * match, so /example and /EXAmplE will return the same page.
  50. * Relying on case-sensitive path matching is not recommended because it
  51. * negatively affects user experience, and path aliases do not support case-
  52. * sensitive matches. The case-sensitive exact match is currently supported
  53. * only for backwards compatibility and may be deprecated in a later release.
  54. * - The 'defaults' section tells how to build the main content of the route,
  55. * and can also give other information, such as the page title and additional
  56. * arguments for the route controller method. There are several possibilities
  57. * for how to build the main content, including:
  58. * - _controller: A callable, usually a method on a page controller class
  59. * (see @ref sec_controller below for details).
  60. * - _form: A form controller class. See the
  61. * @link form_api Form API topic @endlink for more information about
  62. * form controllers.
  63. * - _entity_form: A form for editing an entity. See the
  64. * @link entity_api Entity API topic @endlink for more information.
  65. * - The 'requirements' section is used in Drupal to give access permission
  66. * instructions (it has other uses in the Symfony framework). Most
  67. * routes have a simple permission-based access scheme, as shown in this
  68. * example. See the @link user_api Permission system topic @endlink for
  69. * more information about permissions.
  70. *
  71. * See https://www.drupal.org/node/2092643 for more details about *.routing.yml
  72. * files, and https://www.drupal.org/node/2122201 for information on how to
  73. * set up dynamic routes. The @link events Events topic @endlink is also
  74. * relevant to dynamic routes.
  75. *
  76. * @section sec_placeholders Defining routes with placeholders
  77. * Some routes have placeholders in them, and these can also be defined in a
  78. * module_name.routing.yml file, as in this example from the Block module:
  79. * @code
  80. * entity.block.edit_form:
  81. * path: '/admin/structure/block/manage/{block}'
  82. * defaults:
  83. * _entity_form: 'block.default'
  84. * _title: 'Configure block'
  85. * requirements:
  86. * _entity_access: 'block.update'
  87. * @endcode
  88. * In the path, '{block}' is a placeholder - it will be replaced by the
  89. * ID of the block that is being configured by the entity system. See the
  90. * @link entity_api Entity API topic @endlink for more information.
  91. *
  92. * @section sec_controller Route controllers for simple routes
  93. * For simple routes, after you have defined the route in a *.routing.yml file
  94. * (see @ref sec_register above), the next step is to define a page controller
  95. * class and method. Page controller classes do not necessarily need to
  96. * implement any particular interface or extend any particular base class. The
  97. * only requirement is that the method specified in your *.routing.yml file
  98. * returns:
  99. * - A render array (see the
  100. * @link theme_render Theme and render topic @endlink for more information).
  101. * This render array is then rendered in the requested format (HTML, dialog,
  102. * modal, AJAX are supported by default). In the case of HTML, it will be
  103. * surrounded by blocks by default: the Block module is enabled by default,
  104. * and hence its Page Display Variant that surrounds the main content with
  105. * blocks is also used by default.
  106. * - A \Symfony\Component\HttpFoundation\Response object.
  107. * As a note, if your module registers multiple simple routes, it is usual
  108. * (and usually easiest) to put all of their methods on one controller class.
  109. *
  110. * If the route has placeholders (see @ref sec_placeholders above) the
  111. * placeholders will be passed to the method (using reflection) by name.
  112. * For example, the placeholder '{myvar}' in a route will become the $myvar
  113. * parameter to the method.
  114. *
  115. * Additionally, if a parameter is typed to one of the following special classes
  116. * the system will pass those values as well.
  117. *
  118. * - \Symfony\Component\HttpFoundation\Request: The raw Symfony request object.
  119. * It is generally only useful if the controller needs access to the query
  120. * parameters of the request. By convention, this parameter is usually named
  121. * $request.
  122. * - \Psr\Http\Message\ServerRequestInterface: The raw request, represented
  123. * using the PSR-7 ServerRequest format. This object is derived as necessary
  124. * from the Symfony request, so if either will suffice the Symfony request
  125. * will be slightly more performant. By convention this parameter is usually
  126. * named $request.
  127. * - \Drupal\Core\Routing\RouteMatchInterface: The "route match" data from
  128. * this request. This object contains various standard data derived from
  129. * the request and routing process. Consult the interface for details.
  130. *
  131. * Most controllers will need to display some information stored in the Drupal
  132. * database, which will involve using one or more Drupal services (see the
  133. * @link container Services and container topic @endlink). In order to properly
  134. * inject services, a controller should implement
  135. * \Drupal\Core\DependencyInjection\ContainerInjectionInterface; simple
  136. * controllers can do this by extending the
  137. * \Drupal\Core\Controller\ControllerBase class. See
  138. * \Drupal\dblog\Controller\DbLogController for a straightforward example of
  139. * a controller class.
  140. *
  141. * @}
  142. */