دروپال 8: module.routing.yml جایگزین hook_menu در دروپال 8

در دروپال 7 توسط hook_menu توابع page callbacks همراه با عنوان، آرگومان ها و دسترسی ها تعریف می شدند. در دروپال 8 این عملیات توسط module.routing.yml تعریف می شود.

user.logout:
  path: '/user/logout'
 defaults:
    _controller: '\Drupal\user\Controller\UserController::logout'
  requirements:
    _user_is_logged_in: 'TRUE'
  1. هر route شامل یک id (در اینجا: user.logout) و path (در اینجا: /user/logout) می باشد.
  2. در قسمت defaults تعیین میشود در صورت مچ شدن درخواست با path باید چه اتفاقی بیافتد.
  3. قسمت requirements شروط لازم برای انجام اتفاق مورد نظر است. معمولا شامل اطلاعاتی برای چک کردن دسترسی هم میشود. مانند access arguments و access callback در دروپال 7

مقادیر default

  • _controller
    The specified method is simply called with the specified route parameters, and is expected to return a response.
  • _content
    If specified, the _controller is set based on the request's mime type, and fills the content of the response with the result of the specified method (usually a string or render array).
  • _form
    If specified, the _controller is set to HtmlFormController::content, which responds with the specified form. This form must be a fully qualified class name (or service id) that implements FormInterface and usually extends FormBase. Indeed, form building has also become object oriented!
  • _entity_form
    If specified, the _controller is set to HtmlEntityFormController::content, which responds with the specified entity form (specified as {entity_type}.{add|edit|delete}).
  • _entity_list
    If specified, the _controller is set to HtmlFormController::content, and _content to EntityListController::listing, which renders a list of entities based on the entity type's list controller.
  • _entity_view
    If specified, the _controller is set to HtmlFormController::content, and _content to EntityViewController::view, which renders the entity based on the entity type's view controller.
  • _title
    The title of the page (string).
  • _title_callback
    The title of the page (method callback).

مقادیر requirements

  • _permission
    The current user must have the specified permission.
  • _role
    The current user must have the specified role.
  • _method
    The allowed HTTP methods (GET, POST, etc).
  • _scheme
    Set to https or http. The request scheme must be the same as the specified scheme. This property is also taken into account when generating urls (Drupal::url(..)) rather than routing. If set, urls will have this scheme set fixed.
  • _node_add_access
    A custom access check for adding new nodes of some node type.
  • _entity_access
    A generic access checker for entities.
  • _format
    Mime type formats.

مثال

node.view:
  path: '/node/{node}'
  defaults:
    _content: '\Drupal\node\Controller\NodeController::page'
    _title_callback: '\Drupal\node\Controller\NodeController::pageTitle'
  requirements:
    _entity_access: 'node.view'

برچسب ها: 

افزودن نظر جدید