跳转到主要内容
晨雨 提交于 26 August 2014

定义路由的最简单的方法是创建一个module.routing.yml文件。每个路径被定义为module_name.route_name形式的机器名称(book.render)具有以下属性:routes的结构:

  • path (required): 你可以通过在花括号引用他们,使用的动态特性。(例如,路径: ‘/node/{node}/outline’) 。(路由的URL是以一个反斜杠开头(例,path:’/book’).你可以在大括号里面使用动态属性来引用它们。(例,path: ‘/node/{node}/outline’).这将会通过参数转换传递到控制器/表单里面.(如下).如果你想在路由里面有一个可选的参数可以参看 这里
  • defaults (required): 定义路由的默认属性。提供一个以下指定如何生成输出:
    • _content: 典型的如(classname::method),以返回一个可渲染的数组。这会被渲染进HTML中并作为Drupal主题的主要内容。 Drupal8路由和控制器的示例介绍
    • _controller: 仅返回一个对象,不会被主题化。如Symfony\Component\HttpFoundation\Response object(Another callable returning a Symfony\Component\HttpFoundation\Responseobject. )因为没有HTML(如,XML种子)或仅有一些HTML片断。这会被直接发送,没有主题或区块会被添加。
    • _form: 这一个期望实现Drupal的formInterface。 可以查看Drupal8表单的示例。 
    • _entity_view: 这个值是entity_type.view_mode.在给定的视图模式下,通过路径和渲染模式下查找一个实体。如: _entity_view:node.teaser。 在视图模式下将返回一个{node}的渲染数组。查看实体视图控制器提供的实体视图页面
    • _entity_list: 这个值是entity_type。使用期望实体的EntityListController提供一个实体列表。例:_entity_list: view_mode返回一个视图模式的渲染数组。
    • _entity_form: 和_form相似,但它提供的是一个可编辑的实体表单。实体表单的处理被定义在实体的元数据(注释)中。例如,_entity_form: node.default将会显示节点表单。在node.default中node引用了实体的ID,’default’引用了表单的控制器键。

    一些可选的选项参数:

    • _title (optional): (可选)该路由的页面标题。可以不同于菜单标题。(The page title for the route. May differ from the menu link title.)
    • _title_context (optional): :(可选)用于传递给t()标题文本附加上下文信息。(Additional context information for the title text passed along to t().)
    • _title_callback (optional): (可选)一个PHP调用(通常是classname::method)返回的网页标题的路线。(A PHP callable (typically classname::method) returning the page title for the route.)

    最终,如果任何静态参数被传入控制器中,在默认的数组中提供它。命名其他名字和控制器中方法中的一致。如下。

  • 如果你最后可能添加的可选的参数,因此foo/bar/{baz}也应该让foo/bar的baz值和你在默认数组中设置的值一致。

  • requirements (required): 确定必须满足什么条件才能授予访问路径。是以下一个或多个数组:(Determines what conditions must be met in order to grant access to the route. Is an array of one or more of the following:)
    • _permission: 一个权限字符串(例如,_permission: 'access content’)。A permission string (e.g., _permission: 'access content').
    • _role: 一个特定的Drupal的角色,比如“管理员”。您可以通过指定多个,”或“+”和逻辑(但是使用基于权限的访问限制在可能的时候)。(A specific Drupal role, eg. 'administrator'. You can specify multiple ones via "," for OR and "+" for AND logic. Note that, since the roles available may differ between sites, it's recommended to use permission based access restriction when possible.)
    • _access: TODO.
    • _entity_access: 在一般的情况下,实体是路由的一部分,检查一定的访问级别可以在批准访问(例如,_entity_access:”node.view”)。你也可以指定实体如何应验证(例如,node:\d+)。这是有益的如果你有一个路由/foo/ {node}和/foo/bar的一个节点是节点ID。/ foo/bar不匹配/ foo / {node}因为bar不是数字(见例book.routing.yml)。(In the case where an entity is part of a route, can check a certain access level before granting access (e.g., _entity_access: 'node.view'). You can also specify how the entity should be validated (e.g., node: \d+). This is useful if you have a route /foo/{node} and /foo/bar where {node} is a node id. /foo/bar won't match /foo/{node} because bar isn't numeric (see book.routing.yml for an example).)
    • _custom_access: TODO. See Access checking on routes for details.
    • _format: 用这个来检查请求类型。例如,你可以_format:JSON只匹配请求的接受报头为JSON的请求。(Use this to check the type of the request. For example, you can have _format: jsonand only match requests where the Accept header is json.)
    • _module_dependencies: 可以使用这个键来指定一个或多个模块所需要的服务路径。你可以将模块名称+(加号)的关系,(逗号)的关系。例如,_module_dependencies:’node+search’意味着需要两个模块,如果_module_dependencies:’node,search’需要其中一个就可以。如在info.yml中已经依赖于其他模块,在这里就没有必要再写。然而,可选的依赖,路由是只有那些可选的依赖的模块也使提供的,这是一个有用的选项。(Optionally use this key to specify one or more modules that are required to serve this path. You can combine module names with a + (plus) for an AND relationship or , (comma) for an OR relationship. For example, _module_dependencies: 'node + search'means both node and search are needed, _module_dependencies: 'node, search'means either node or search are needed. If your module already depends on other modules for its behaviour (via info.yml dependencies), there is of course no need for specifying the dependency here as well; however for optional dependencies, where routes are provided only if those optional dependent modules are also enabled, this is a useful option.)
    • _csrf_token: TODO. See Access checking on routes for details.
  • options (optional): 附加选项如何路由应相互作用。常见的选项:(Additional options on how the route should interact. Common options are:)
    • _access_mode: (ANY / ALL (default) 如果给出了多个需求(requirements),指定他们应该如何联合。(If multiple requirements are given, specifies how they should be combined.) If you choose ALL, all requirements access checkers have to ALLOW access, otherwise on ANY just one has to ALLOW. If any of the access checkers return KILL, access will be denied immediately.
    • _theme: TODO
    • parameters: 如果请求中参数不匹配任何实体名,你可以翻译(转换)这个参数到一个实体。并且你不能使用两个相同的参数。对于没有匹配的实体,你可以使用{user}。( If your parameter name from the path doesn't match any entity name you can translate the parameter to an entity here. And, as you can't have two parameters with the same name, let's say {user}, you can map the unmatched entity here.)
      • second_user:  路径的参数名称,在这种情况下它的第二个参数。(The parameter name from the path. In this case it's {second_user}.)
        • type: 这是我们告诉什么实体使用。在这个例子中,我们就可以写的entity:user,给用户实体匹配参数。 (This is where we tell what entity to use. In this example we can just write entity:user, to map the parameter to the user entity.)

这是一个更复杂的注释的例子:

book.routing.yml

# Each route is defined by a machine name, in the form of module_name.route_name.
#
book.render:
  # The path always starts with a leading forward-slash.
  path: '/book'
  # Defines the default properties of a route.
  defaults:
    # For page callbacks that return a fully-themed HTML page use _content.
    _content: '\Drupal\book\Controller\BookController::bookRender'
  # Require a permission to access this route.
  requirements:
    _permission: 'access content'
book.export:
  # This path takes dynamic arguments, which are enclosed in { }.
  path: '/book/export/{type}/{node}'
  defaults:
    # Because this route does not return HTML, use _controller.
    _controller: '\Drupal\book\Controller\BookController::bookExport'
  options:
    # Ensure *both* requirements below are checked to grant access to the route.
    _access_mode: 'ALL'
  requirements:
    _permission: 'access printer-friendly version'
    # Ensure user has access to view the node passed in.
    _entity_access: 'node.view'

将参数传递给控制器(Passing arguments to controllers)

参数传递给控制器 在default节中,不一定所有传递给控制器的参数都会使用_下划线开始命名。恰当的命名你的控制器参数如下所示:(All keys under the defaultsection which do not start with an underscore will be passed in as arguments to the controller. Name your arguments appropriately for the arguments of the controller. For example a routing.yml file with the following:)

example.content:
  path: '/example'
  defaults:
    _content: '\Drupal\example\Controller\ExampleController::content'
    custom_arg: 12
  requirements:
    _permission: 'access content'

将传递参数$custom_arg给控制器,所以你的内容的方法可以使用参数$custom_arg:

<?php
  // ...
  public function content(Request $request, $custom_arg) {
    // Now can use $custom_arg (which will get 12 here) and $request.
  }
?>