15. 路由系统Route

drupal8 龟龜龛龘龔龓龏龋龊龆龄龁龓龏齽齻齸齵齴齲龆龛齯龓龏龄齬齫齩 Symfony 龘龔齝齛龄齘齕齔齑齏齌齋齈 Symfony 龛龘龔齝齛齅

Symfony 龘龔齝齛龛齁鼽鼻鼹鼷鼴齻鼰 http://symfony.com/components/Routing

龘龔齻鼏鼋鼇鼅鼂齻齸齵齔黿黾黼黹黷黶龔黲黰黮黪龛龓龏龄齬黷黶黨黮黪黥黤龛黰黡龄齸黝齴齲黜龛龓龏龘龔黛黚默黔黓鼽鼴黑黶龛黐黎黍黋黇黃麿黲麽黮黪龄齴麼麻麸龛 MVC 龄麬麨龛 CI 麦麣鼂龁鼽鼴齽麢麠麟麜麛麗麔麒麗麏麌麈麄麽黃麿黥黤黰黡龄麁鹿 drupal8 龟龜龛龘龔龓龏鹽麻龊龆龄鹹鹶鹴齩鹳龄齬鹶鹯黓 url 鹭鹪黑鹩鹨黿龄鹧鹦齕黔黓齔黿鹤鹠麗鹞鹛麛鹗黪鹔鹑鹐麗鹍鹋鹈鹄鹃麌麗鼽鼴鸿鸻龛鸹鸸黐黎鸴鸴麽麟麜龄鸲鸱鸮龟龜鸫鸩鹪鸨黼鸥龛鸢鹐鸞龄鸜龓龏鹈鸿黑鹩鸘鸗龄齻齸齵鸕鸔鸮龛龘龔龓龏齅

drupal8 龘龔龓龏鸑龛龋鸍鸋龄鼂齻鸊齸齵齔黿鸈鸇鸅齸齝鸃鸁龄鸲黝鸃鸁麢麠鷾鹨龛麟麜麛麗鷼鷺鷸鷵麗鷴鷲麟麜鸴鸴鹈鹄龄龓龏鸊鸲黝鸃鸁鷰鷬龁齔黿黓鷪龛 $request->attributes 鷚鷗麢齽龄齕鷔黹鷐鷸鷵鸘鷏麗麟麜麛鸘鷏黥黤龟龜齅

龁鷎龓鷋龛鷈鷆鷃 Drupal8 鶼鶻鸻鷏鶹鶶鶳黮黪黥黤 HttpKernel drupal8 鶨鶧黥黤鶦鶤齽鶢鶟鶞鶚龓龏鶨鶧龛鶗黥黤龄龘龔龓龏齻龁鶶鶳鶔鶒鶐 'kernel.request' 鶉齛鶈鷸鸜鶨鶧龛龄鶆鶅龛鶂鵿麛鸅 router_listener 龄龁鵹 onKernelRequest 鹤鹠齽鶨鶧黥黤鵶鵴龘龔龓龏龄鵱齵龘龔龓龏齕鵰齑 router 鵯鸅 api 鵮鵭龄 drupal8 鵩鵦鵢鵡龁龘龔齽鵶鶧龄鵩鵦鵝黪鵰齑龛鵰齑 id 鸅鼰 access_manager 龄龔齩鵩鵦麟麜齻齸鵙龋龆龛鹈鹄龄鵗鵕鵑齸齵鶗鵐鵍鸘龄鷎鵋鵊鵍龘龔龓龏龄鵆鼂齻鹭鹪鷴鷲鵢鵡龛龘龔龓龏龄鵰齑麨鸅鼰 router.no_access_checks 龄鵄鵂齌鵀龘龔龟龜鴿鴼麽鴹齸鴹鴶鼋黶鴵齸齵龘龔鼰

龁鶶鶳鴲麏鵙齽龟龜 name.routing.yml name 鸅麏鵙麨鶦麽黶鴵鴯鴮鴪鴦龛龘龔龄龁鸲齵鼻齛齽鹦齕黶鴵鴤齵龘龔龄鴠鴞鴹齸鴹鸋齵龘龔齻鴶鼋黶鴵龛龄鸲齻齸齵鸕鴞龛鴝齯龄麸鴜鴛鹈鹄鴘鼰

齬鵂鷬鴔龁 yaml 鼻齛齽龄齏鴐鴍鴌 yaml 黐黎龄鴈鸸 yaml 鸘鷏麛默鴆鴄龄鴠鴞麽鸘鴀鳾齵鸿鸻鼰

yunke.content:
  path: '/yunke/{book}/{page}'
  host: "{subdomain}.example.com"
  schemes:  [https]
  methods: [GET, HEAD]
  defaults:
    _controller: '\Drupal\yunke\Controller\book::show'
    _title: 'Hello World !'
    book:'drupal'
    page:1
    subdomain: m
  requirements:
    _permission: 'access content'
    page: \d+
    subdomain: m|mobile
    _format: 'json|html'
  options:
    _route_filters: 'yunke__route_filters'
    no_cache: TRUE

路由定义中各元素含义:

yunke.content 鼰鸨鳽龘龔麨龄龁龓龏鹈鸿龜鵯鳹麛麨龄鹗鸨鸲齸鳸龘龔龄齸齵 yaml 鼻齛齽鹦齕黶鴵鴤齵龘龔龄鹯 drupal 鵱齵龓龏齽龘龔麨齏鴐鳵齸龄齕鴠鸅齬龛鳳齸鳱黋黇鼰

path 鼰齏鴐鳭龄鸢鹐鸞鳩龄鳧齏鴐齕 / 鵑鳤龛齸齵龘鳢龄鹦鸃鳡龛鸿鸻龜鳟鳜鳙麢鳖麽龄鳕鳔齸齵龘鳢鸃鸁龄鳓鴞龛鸢鹐鸅鳐鳍鹐麨龄鳳齸齵龘鳢鳌鹶鸮龟龜龘鳢鸃鸁齅

host 鼰鹦鳈龄鸢鹐鸞鳩龄鵦黶鹦鳅鳄鹳龘龔龛鶗鳹麨龄鳀鸱鹦齕龟龜 {} 麽麢麠鹦鸃鸿鸻鳕鳔齸齵龘鳢鸃鸁龄鲹鲸黶鸸鸫鸩齅

schemes 鼰鹦鳈龄鷵齝鳩龄鹦齕龟龜龛鲵鲴麨龄鲹鲸黶鸸鹶鵦麜齅

methods 鼰鹦鳈龄鷵齝鳩龄鹦齕龟龜龛齔黿鹤鹠麨龄鲹鲸黶鸸鸫鸩齅

defaults 鼰齏鴐鳭龄鷵齝鳩龄鹗鸨鷼鷺鳩龄鳓鴞鹦齕麢麠龓龏鷼鷺鳩麼麟麜麛麗鹔鵐鸴龄鲱龘鳢鸃鸁鷼鷺鳩鶫龜鳐鳍鹐麨鲸黶鶦齅

requirements 鼰齏鴐鳭龄鷵齝鳩龄鲰鲮鳈鳭龄麢麠鵩鵦鷾黿麗龘鳢鸃鸁鴐黓鲭龛鸹鸸黐黎麗齔黿鹈鹄黐黎鸴鸴齅

options 鼰鹦鳈龄鷵齝鳩龄鷎鳸龘龔龛鷼鷺鳈鳭龄龜鲪龘龔龓龏鷎鲦鲣鷔鲟鵕麈麄龄齴麼鴯鳡龘龔鲛鲘麛麗鲔鷬鷚鷗鸴鸴齅

来看一看以上元素可能的值(第二级值)

defaults 鸮麢麠齕鴠鳩鼰

_controller: 鲸黶齸齵鲐鲍 ( 鲇麻麸龛齻 classname::method 龄齬鹶齻龘鳢龄鹿齻鲆鸕鵦黶龛麨鸢鲂鲀鹃麨龄默鱽鴯鳡鱹鱶麛鱹鱶 ) 鹦齕鱱鲐齸齵鱰鱬鷵齝龄齬鸊鱽麏鱨鱰鱬龄鵆鹦齕鱱鲐齸齵鱤鲭黓鷪龄齬默鱡鵮鶒鱠鲪鷃鱞鱚

_form: 鲸黶齸齵 Drupal\Core\Form\FormInterface 龛鱖鱒

_entity_view: 鲸黶 entity_type.view_mode. 龜鲪鶐龛麔麒麏黎鵡鱊鱖鱉鱆鱰鱬

_entity_list: 鲸黶 entity_type

_entity_form: 鹃鱅 _form 鹯鸅鱖鱉鲣鷔齸齵鱁鰿鸨

_title ( 鹦鳈 ): 龘龔黹黓鲭鰾鴞龛鹔鵐

_title_context ( 鹦鳈 ): 鲟鵕鹔鵐鼻鷎龛鹍鴠鼻麈麄龄齬鹋鰺鲪 t() 鰸鷵

_title_callback ( 鹦鳈 ): 齸齵鲐鲍龄龜齩鰵鰳 _title

鹳鵕鹦齕鲸黶龘鳢鸃鸁龛鷼鷺鳩龄鰱齔黿龘鳢鹶鸮鲣鷔龘鳢鸃鸁龛鳩鶈龟龜龄鵆鹦齕鲟鵕鲣鷔麟麜麛鷾鹨龛鸫鸩鷸鷵鳩龄麨鸢黓鲭麟麜麛鰮麨齽龛鷸鷵麨龄鰫鰪鰩鴦龄鹈鸿龟龜鰨鰤鳹麜黮黪龄龁鸲鳓齘鹪鹶齻齕鴠鰣鰟鵑鳤龛鸃鸁鰞默鹋鲪麟麜麛齅

requirements 鸮麢麠齕鴠鳩鼰

_permission: 鵩鵦鸢鹐鸞 ( _permission: 'access content'). 鴤齵鵩鵦鹦齕龜鰛鳙 ',' 鸻鰙龄齬鵂齻 AND 鴦龓龄鵆鹦齕龜鱹鳙 '+' 鸻鰙鸨鳽 OR 鴦龓龄鹪鰌鰨鱡鰈鹯鼂齻鸲鸱龛龄鵩鵦鸢鹐鸞黶鴵龁 my_module.permissions.yml 龄鰆麸 https://www.drupal.org/node/2311427

_role: 鲸黶龜鱞鴿鯹龄麼 'administrator'. 鸮鲸黶鴤齵龄鳀鹍龄龁鯶鰌鲀鴿鯹鹦鸮鹶齸鸱龄鯲鯮龟龜 _permission 麽麟麜鵩鵦

_access: 麼鯫黛鯨鸅 "'TRUE'" ( 鶆鸩鸋鯙鳙 ) 龄鯖鼋龁鸫鸩鯔鯐鹦鷴鷲

_entity_access: 麼鯫龘龔鲸黡齸齵鱖鱉龄鸮鵢鵡鯌鷴鷲鳱鯉 ( _entity_access: 'node.view')

_custom_access: 鴯黶鴵鷴鷲鵢鵡龄鰆麸鷴鷲麟麜

_format: 鲸黶齔黿鹃麌鶫鯆鯂鯁鮽鷵鮹龛鹃麌鶦龄齴麼 _format: json 鸊鵊鵊鳅鳄 Accept header json 龛齔黿

_content_type_format: 鲸黶齔黿龛鹈鹄鹃麌鶫鹍鹋鷵鮹龛鹃麌鶦龄齴麼 _content_type_format: json 鸊鳅鳄齔黿 Content-type header json 龛齔黿齅

_module_dependencies: 鹦鳈龛龟龜鯌鮶鮳龛齸齵黍鴤齵麏鵙龄鴤齵 module names + 鸨鳽 AND 龄龜鰛鳙鸨鳽 OR 龄鶆鸩鸲齵鴲鹍鴞龛鹶齸鸱 . _module_dependencies: 'node + search' 鸩鮯黨 node search 鰞鷾鹨 , 麼鯫龁 info.yml 齽鶢鶟鲸黶鮶鮳鴦龓鼂鹶鷾鹨鸲齵鳈鳭

_csrf_token: 鮮鯶鮫鮩黛鯨龄 'TRUE' 龄鰆麸龘龔鷴鷲鵢鵡

鹳鵕鹦齕麢麠黓龘鳢鸃鸁龛黐黎鵦黶龄龟龜鸹鸸鸨黼黎鸅鳩龄龘鳢鳐鳍鹔鹑鹐鸅鮥

options 鸮麢麠齕鴠鳩鼰

_access_mode: 鳩鸅鼰 ANY / ALL 鷼鷺鸅 ALL 龄鸲齵鳈鳭鶢鶟鱽鮞鮚鮙龄鱒龁鮖齻 ALL

_admin_route: 齻鴈龟龜鵝黪鶗鵐

_theme: 鲸黶鶗鵐

no_cache: 黛鯨鸅 'TRUE' 鸨鳽鷎龘龔鮕黾龛齔黿鮑鯫鹶鹦鲔鷬

parameters: 鷸鷵鸈鸇龄鹳鳈鳭鹦齕鸊龘鳢鳐鳍鸃鸁龛鳩鸈鸇鸅齸齵黓鷪龄麸鴠鼻

路由的一些不规则用法

齴麼龁 requirements 齽鲸黶 _method 'GET|POST' 麽麟麜齔黿鹤鹠鵆齻鹦齕龛龄鹯鹶鹨鸲鸱龜龄齬龁 Symfony2.2 齽鼂鮈龜鮙龄龁 3.0 鮃鷎齽默鲆鸕鮞鮚龄鸲鳓鷋鶐鮁齻鸅魽鸘魻龄齔鴍鵀鹍鴞龛鴝齯麽黶鴵龘龔齅

麼魹黶鴵龘龔鹦齕鷸鴹齁鹤鼻鼹鼰 https://www.drupal.org/docs/8/api/routing-system/structure-of-routes

齕鹍鼂齻龘龔龓龏魷魶龜鱞龟龜魴鴞龛鹈鹄鮙龄鯖鼋龁龓龏齽龘龔龓龏齻麼魹鱖鱒龛鼇鼅魱鷐

齸齵鲆鵱龛龘龔龓龏鲭魰齻魬黡龛龄魩鹦齕龟龜齔黿鮕黾龘龔麈麄龄鵆鹦齕龜龘龔麈麄鰳鳔齸齵 URL 龄龁 drupal 齽鼂齻麼鹳齅

鸲鸱龘龔鼂鹪魦齵齫鷎鸿鸻鼰

龘龔黓鷪鶫鷰鷬龘龔麈麄鶦麗鳅鳄麛鶫龜齕鳅鳄齔黿黾齸齵魢鱉龛龘龔黓鷪鶦麗 url 鰵鰳麛鶫龜龘龔黓鷪鰳鳔齸齵 url

Symfony 龛龘龔齝齛鼂齻魞魝鸲魦齵鸿鸻魜鵑龄 drupal 鼂齫齩鸲齵齝齛龄鹗鶻鳍齩 \vendor\symfony\routing

麽鴹齸鴹鸲齵鼻齛魛鳓鴞龛鼻齛鼰

Route.php 鼂齻龘龔黓鷪龄龜齩鷰鷬龘龔麈麄龄鵄鵂龁鹍鴞黶鴵龛龘龔麈麄鲇魘默鸈魖鸅鸲齵黓鷪

RouteCollection.php 鼰龘龔魕黓鷪龄龓龏齽默鹪龋鴤龘龔鰟龘龄鴤齵龘龔黓鷪鷾鹨齬麽龏齸鵝黪龄鳅鳄麛鼂龁鸲齵龘龔魕鳓鴞魔鱊鸮鳅鳄龛龘龔黓鷪龄 URL 鰵鰳麛鵆默魒鮹龘龔麨鵀鳓鴞鮽鮕齸齵龘龔齅 鳅鳄麛鴲鰵鰳麛魏鳍齩齸齵鼻齛魛龄鳓鴞鹪鹶鳀龜魋龛鴤齵鳅鳄麛鴲鰵鰳麛齅

鳅鳄麛齻鴶鼋魉鵯龛鼇鼅齬鵹鱖齻龜龘龔魕黓鷪鳓鴞龛鳾齵龘龔黓鷪黰魈魅齻鴈鳅鳄齔黿齅鴶鼋魈魅鼅齬鸊龘龔黓鷪鷰鷬龛龘鳢麈麄鴲魃麨麈麄魏鰳鳔黓鲭龛鸹鸸鸨黼黎龄魀龜鸹鸸鸨黼黎黰鳅鳄齔黿龛 url 鴹齻鴈鳅鳄齅鰳鳔鸹鸸鸨黼黎鸲齵魉鵯龔龘龔黓鷪鱁鬼麛鵶鶧龄齬鷾鹨鬺鬶龘鳢鸃鸁龛鸹鸸鲰鲮龄鸲鼂齻 RouteCompiler.php 龛魉鵯齅鰳鳔黹龛龘龔黓鷪鬵鬱鱁鬼黹龘龔黓鷪龄鵆鼂齻 CompiledRoute.php

齴麼龘鳢鸅鼰 $path="/new/{cms}/yunke_{type}/show"; 鲰鲮鳸齛齻鼰 $requirements=["cms"=>"\S+","type"=>"[a-zA-Z\d]+"];

鰳鳔鶫鱁鬼鶦龛鸹鸸鸨黼黎鸅鼰 #^/new/(?P<cms>\S+)/yunke_(?P<type>[a-zA-Z\d]+)/show$#s

鶗鳹鸅鼰 $host="{sub}/baidu.com"; 鲰鲮鳸齛齻 $requirements=["sub"=>"m|www"];

鰳鳔鶫鱁鬼鶦龛鸹鸸鸨黼黎鸅鼰 #^(?P<sub>m|www)/baidu\.com$#si

鲰鲮鳸齛鼂齻龁黶鴵齽 requirements 鳭齽鲸黶龛鸹鸸齅鹶鬚鬙鸹鸸鹦鸮鹶黪鸘 (?P<cms>\S+) 齻鼏鼋鸩鬘龄齬鸨鳽龁鳅鳄鮑鯫鸊鸲齵齯齝鳅鳄黾龛鹈鹄鬔鳩鲪齸齵鬵鬱 cms 龛鸃鸁齅龓龏齻龟龜 preg_match 鰸鷵鳅鳄龛龄鸲鵆鼂齻鸅鼏鼋鸮鸊 utl 齽龘鳢鸃鸁黓鲭龛鳩鹋鲪麟麜麛龛鶶鶳鬐鬎齅鮚鮙鸹鸸鳅鳄鵕鹧默鵢鵡齔黿鹤鹠麗齔黿黐黎鸴鸴鹈鹄龄鸅鹳鹪齸齵鬵鬱齔黿鹍鴠鼻龛黓鷪麽鲣鷔齔黿黓鷪龛鸲黝麈麄齅鵆鼂齻 RequestContext.php 龛魉鵯龄齬鬊鬊龜齔黿黓鷪麽鬇鬅龄鯖鸅鼏鼋鹶鱡鵮龟龜齔黿黓鷪鼇鼅鬁髽齻鹦齕龁鹭鹪齔黿黓鷪龛鯔鯐鴠髻髸齔黿鹍鴠鼻龄黓髴龛髰髭鵆鹶默髬鱤黾齔黿黓鷪齅

鮖鶹鳅鳄麛默鵀龘龔魕鳓鴞鳈鮽齸鳸龘龔龄麼鯫鹪鴤鳸龘龔鸸龟龜髫齌鳱龄 url 鰵鰳麛齴齲鸍鸋鸲鳓鹶鬱鵍鸘齅龁 Symfony 龛龘龔齝齛齽鹧鲣鷔鮙齸齵 Router.php 龄齬齻鬱鼏鼋龜龛鼇鼅齬齻鸲齵齝齛龛髧髥髤龜齅

鶞黾鸲鳓魽鹦鸮默鹪齸齵髢鷲鼰鸹鸸鳅鳄齻鹽麻髟髜鶼鴲鶈鲀龛龄龁 drupal 齽鹪鳔髚鹍髗龛龘龔龄鸲鸱鬱龓龏鹶齻默鹽麻鹽麻龛髓髒鼅髐鱖鹶鴄龄齘齕 drupal 鵶鹿龜黾鮙 Symfony 麦麣鶫麦麣鹶齻齝齛鶦龛龘龔鸿鸻龄齬鬵鬱鳡髌龘龔龄龔齬麽鸘黷鸲齵鷲鵐龄鹗鶻鳍齩鼰 \vendor\symfony-cmf\routing

symfony框架路由

龁鸲鳓鹪齸齵 DynamicRouter.php 龄髊鸅鳡髌龘龔龄黓鲭黨 Symfony 龘龔齝齛齽龛 Router.php 龄齬齻麦麣龘龔龛髧髥髤龜

鸅鮙髇鱹鱖龜鰵鰳鮙齸齵鬵鬱龘龔鲣鷔麛龛髃骿龄齬龜鵮鵭鼰 RouteProviderInterface.php 骽骻龄骸麨鬘鴵齬鼂齻龜麽鰵鰳龘龔魕黓鷪龛黓鷪龄鹍鴞鶞黾鮙 drupal 齽鹪骵鴤龛龘龔龄鸅鮙骲骱骰龁鹦鸮鳅鳄龛龘龔龄鲣骯鷗鸮龄龓龏鹪骮鹤鴞龛鬺鬶鼰髫魖龘龔鲣鷔麛麗龟龜龘龔鲛鲘麛龄鸲齵髫魖鶗鹨龔龘龔鲣鷔麛鲆鳔龄麽鴹齸鴹 drupal 龛龘龔鲣鷔麛鼰

\core\lib\Drupal\Core\Routing\RouteProvider.php 鼂齻 drupal 龛鶶鶳龘龔鲣鷔麛龄齬魒鮹齔黿黓鷪鵀鷵鮹骬齽鮽鶐鹦鸮默鳅鳄鹍龛龘龔龄齬齻鴶鼋魉鵯龛鼇鼅鵄鵂齌鴹鴹龘龔鸨龛黛黚鼰

龘龔鷵鮹骬鸨

龘龔鸨鸅 router 龄鵹鸢鳌鮑髻鲱鸩鴵麼鴠鼰

name 鼰鶗鮥龄龘龔龛鳹麛麨

path URI 龘鳢

pattern_outline 鼰龘龔龘鳢龛龆骨麏黎龄鹽魢鱉麏黎 龄龜齩骥骣骡鳈鹦鸮默鳅鳄龛龘鳢

fit 鼰鹗鸨鸲齵龘鳢鹪鴤魢鱉龛齸齵鷵鳩龄鲰鸴齩龘龔龛髫齌鳱

route 鼰龘龔黓鷪 Symfony\Component\Routing\Route 龛鰪鷋魖鸨鳽

number_parts 鼰龘鳢龛鳌鷵鶫黓鲭黨 path 鸢鳌鸮鱽 / 鸻骝鳔龛鳌鷵鶦

鶶鶳龘龔鲣鷔麛魒鮹齔黿黚骜鶐 URL 鳌骚鴼鶫龜 / 鸻骝龛鳌鷵鶦 number_parts 麗鲱鹦鸮鳅鳄龛龆髃麏黎 pattern_outline 龄魶鲛鸲骮鳭鵀鷵鮹骬齽鵡鱊鹦鸮龛龘龔龄鶶鶳鹗鶻麼鴠龄齬鳍齩 getRoutesByPath 鹤鹠齽鼰


    $parts = preg_split('@/+@', $path, NULL, PREG_SPLIT_NO_EMPTY);//计算段数
    $ancestors = $this->getCandidateOutlines($parts); //计算大概模式
    $routes = $this->connection->query("SELECT name, route, fit FROM {" . $this->connection->escapeTable($this->tableName) . "} WHERE pattern_outline IN ( :patterns[] ) AND number_parts >= :count_parts", array(
        ':patterns[]' => $ancestors, ':count_parts' => count($parts),
      ))->fetchAll(\PDO::FETCH_ASSOC);  //查询路由

龁鸲齵鹤鹠齽鹧默魒鮹 fit 鳩黓龘龔骒鰪龄 fit 鳩鹗鸨龘龔龛鰆骏黤鴼龄验鰆骏髫齌鳱验骯齅黚骜魉鵯齻骉骈龛龄齘齕龘龔鲣鷔麛默鸊鵡骔鮑鯫鷬鵴鲔鷬龓龏齽龄鴠骇鷴鷲鹶齏魀黚骜龄鲔鷬鸨鸅 cache_data齅

drupal路由系统的调用过程

router_listener 鼰鵀鸲鳓鵑鳤鮶骇鲍龜齕鴠鵰齑鼰

router Drupal\Core\Routing\AccessAwareRouter 魢骅鷴鷲鵢鵡龛龘龔 Router骁

router.no_access_checks Symfony\Cmf\Component\Routing\ChainRouter 驾驼龛驹黎 Router 龄齬鵹齽鹦齕麢麠龋鴤 Router骁

router.dynamic Symfony\Cmf\Component\Routing\DynamicRouter 鸲鼂齻 Symfony 麦麣龘龔龛鳡髌龘龔鮙骁

router.matcher Symfony\Cmf\Component\Routing\NestedMatcher\NestedMatcher 麦麣龘龔龛鳅鳄麛骁

router.route_provider Drupal\Core\Routing\RouteProvider 鲇魘鵀鷵鮹骬鯁鮽龘龔鷵鮹龛鶶鶳龘龔鲣鷔麛骁

鲣鷔麛鸊龘龔魕黓鷪鹋鰺鲪鶶鶳鳅鳄麛 router.matcher 龄鳅鳄麛龁鲇魘鶨鶧鳅鳄驶默鲭龜龘龔鲛鲘麛黰鵶齸骣骲骱鹶鹦鸮龛龘龔齅

路由过滤器

龓龏鷼鷺鲣鷔鮙魦齵龘龔鲛鲘麛鼰

request_format_route_filter Drupal\Core\Routing\RequestFormatRouteFilter 鶦鵢鵡黐黎齻鴈驵驴骁

method_filter Drupal\Core\Routing\MethodFilter 鶦鵢鵡齻鴈驵驴鹤鹠龄鷼鷺龛麼鯫鲸黶 GET 鹤鹠鸸驱麠龛驵驴鮙 HEAD 鹤鹠骁

content_type_header_matcher Drupal\Core\Routing\ContentTypeHeaderMatcher 鶦鲭龜齩齘鹪龘龔龄魒鮹鹞鹛麛鹍鹋鷵鮹龛鹈鹄鹃麌麽鲛鲘龘龔龄 GET 鹤鹠鹶默鹍鹋鷵鮹龄齘齕鹶鰵鰳鲛鲘骁

鲛鲘麛鮚鮙鲛鲘鸔鸮鵕龄鵆默驰鲣鷔麛齸鸱黓龘龔骒鰪龄验鰆骏龛龘龔髫齌鳱验骯骁

龜鱞鹦齕黶鴵鲟鵕龛龘龔鲛鲘麛龄鱖鱒 Drupal\Core\Routing\RouteFilterInterface 鵮鵭鱆鲪鶐鵰齑鹔鰮 route_filter 驯鹦龄龘龔鲛鲘麛鵰齑黶鴵鷸鬺麼鴠鼰

  method_filter:
    class: Drupal\Core\Routing\MethodFilter
    tags:
      - { name: route_filter, priority: 1 }

龘龔鲛鲘麛龛驫魕魉鵯龔鹄麛鱁鬼驪鳌鲆鳔齅

鱁鬼麛鸅 \core\lib\Drupal\Core\DependencyInjection\Compiler\RegisterLazyRouteFilters.php

鶟鲛鲛鲘黹龛龘龔魕黓鷪鱠鵴鵰齑 router.matcher.final_matcher Drupal\Core\Routing\UrlMatcher 鶦鶨鶧鲇魘龛鳅鳄齅

鶨鶧鲇魘龛鳅鳄黹鳅鳄麛鱱鲐齸齵鷸鷵鷵齝鲪 router.dynamic 龄鹳鶈 router.dynamic 黓鷸鷵鷵齝鲭龜龘龔驦龊麛鼅

路由增强器

龜齩髰髭龘龔鳅鳄黹鮕黾龛鷸鷵龄黍驦鱹黍驣鮚黍髰髭龄鸲鸱驠龘龔龓龏鮕黾髇龆龛驜驚鷗龄龓龏鷼鷺鲣鷔鮙齕鴠驦龊麛龄齕鹄麛鵰齑鹤黎龟龜龄齕鴠鸅鵰齑 ID

route_enhancer.param_conversion:Drupal\Core\Routing\Enhancer\ParamConversionEnhancer
route_enhancer.form:Drupal\Core\Routing\Enhancer\FormRouteEnhancer
route_enhancer.entity:Drupal\Core\Entity\Enhancer\EntityRouteEnhancer
route_enhancer.entity_revision:Drupal\Core\Routing\Enhancer\EntityRevisionRouteEnhancer
field_ui.route_enhancer:Drupal\field_ui\Routing\FieldUiRouteEnhancer

龜鱞鹦齕黶鴵鴯鴮龛驦龊麛龄鷸鬺麼鴠鼰

  route_enhancer.entity:
    class: Drupal\Core\Entity\Enhancer\EntityRouteEnhancer
    tags:
      - { name: route_enhancer, priority: 20 }

鷾鹨鱖鱒驦龊麛鵮鵭龄鱆鲪鶐鵰齑鹔鰮 route_enhancer

鵰齑鹄麛鱁鬼麛 Drupal\Core\DependencyInjection\Compiler\RegisterLazyRouteEnhancers 默鸊齘鹪驦龊麛驫魕鱆鹋鰺鲪鵰齑 route_enhancer.lazy_collector 龏齸鵝黪齅

龘龔鳅鳄黹鱱鲐龛鷸鷵鶟鲛驦龊麛黮黪鲇黹鱱鲐黾鵰齑 router AccessAwareRouter 鶦齽龄齬鸊鵹鷰鷬龁 $request->attributes 齽齅 router 齻齸齵魢骅鷴鷲鵢鵡龛龘龔 router 龄齬默鲍龜鷴鷲鵝黪麛鶫鵰齑 access_manager) 鶨鶧鷴鷲鵢鵡龄鱆鸊 access_manager 鱱鲐龛鮑鯫齸鱆鷰鷬龁 $request->attributes 齽龄麼鯫鷴鷲鹶鸮魶鲛鸸驖鶐鷴鷲驕驓驏麻齅麼鯫魶鲛鸸黥黤鲐黾鶗驎黥黤龄龘龔龓龏髤鶧鲆驊龄鷰鷬龛龘龔鷸鷵鸊默鱽黹鷐黥黤龟龜鶫鷸鷵鸘鷏鲱麟麜麛鶦齅

齕鹍鼂齻 drupal 龘龔龓龏龛鸕鸿鮙龄龔齩鵋驆鹪鵦龄骵鴤骏驄鱆鲹鲣黾龄鹯驂驁鲭魰鹪鮙齸齵龆髃驀鷪齅

路由参数转换

drupal8 鹪齸齵鷸鷵鸈鸇齯龓龏鹦齕鸊龘鳢齽龛鸃鸁鸈鸇鳔黓鲭龛黓鷪龄齴麼鸊 /node/{node} 齽龛 {node} 龛鳩鸈鸇鳔齸齵 node 黓鷪龄麁黹魀鹋鰺鲪麟麜麛龄鸲齻麼魹鱖鱒龛鼇鼅鼂齻龜鮙 ParamConversionEnhancer 鸲齵龘龔驦龊麛龄龟龜鹍鼻鲣黾龛龘龔鳈鳭齽龛 parameters 鳩麽鬱鸲齛鶉鯔龄龜鱞鹦齕龁龘龔黶鴵齽黷黶鷸鷵齻鴈鸈鸇龄齕鲱麼魹鸈鸇龄鴠鴞鴹齸鴠麼魹黶鴵鼰

  options:
    parameters:
      var_name:
        tempstore: TRUE
        type: entity:view

齕鹍黶鴵齽 var_name 齏鴐齻鳐鳍鹐麨龄齬龛齯黋黇鹦齕龜麽黃麿龟龜騿齸齵鷸鷵鸈鸇麛龄鵹齽鹦齕龟龜 converter 鮥麨鶚髐鲸黶齸齵鷸鷵鸈鸇麛龄齬龛鳩齻齸齵鹄麛鵰齑 id 龄魰鵰齑齏鴐鱖鱒 Drupal\Core\ParamConverter\ParamConverterInterface, 麼鯫鹭鹪龟龜 converter 鶚髐鲸黶鸈鸇麛龄鯖鼋龟龜龓龏鲣鷔龛齘鹪鸈鸇麛鮶骇魒鮹鸲黝齯黋黇麽黃麿齻鴈鹦齕髤龜龄麼鯫鹦齕鸸髤龜龄鸈鸇麛騼髫齌鳱骒鰪鮁髤龜齸齵齅鴠鴞麽鴹鴹鷸鷵鸈鸇龓龏龛鱖鱒鼰

龓龏龟龜 Drupal\Core\ParamConverter\ParamConverterManager 麽鵝黪鷸鷵鸈鸇龄鹄麛 id 鸅鼰 paramconverter_manager 龄龁鹄麛鱁鬼驪鳌鸊驫魕齘鹪鹔鰮麨鸅 paramconverter 龛鵰齑鱆鶆鵴黾齬鳓鴞龄鸲黝鵰齑鶫鵆鹦齕鬵鷸鷵鸈鸇麛鶦騹鱖鱒鮙鹍鼻鲣黾龛鷸鷵鸈鸇鵮鵭龄龜鱞鹦齕鴯黶鴵鷸鷵鸈鸇麛龄鷾鹨鲪鶐 paramconverter 鹔鰮龄鹦鳈鲪鶐髫齌鳱齅

鲆鳔驫魕魉鵯龛鱁鬼麛鸅鼰 \core\lib\Drupal\Core\DependencyInjection\Compiler\TaggedHandlersPass.php 鶫鹳黮鲪鵝黪麛鶆鵴鸈鸇麛鶈龟龜 addConverter(ParamConverterInterface $param_converter, $id) 龄鵹齽龛 $id 鷸鷵鼂齻鸈鸇麛鵰齑 id 龄齔鴹鱁鬼麛龛鱖鱒鶦

鶟鲛 ParamConversionEnhancer 鸲齵龘龔驦龊麛龛黮黪龄龘龔龓龏鱱鲐龛鷸鷵鶢鶟齻鱽黮黪鲛龛鮙龄鹯龓龏鱆鹶默騵騲鸈鸇鶹驶龛鳩龄髴鵂鸊鴔鷬龁 _raw_variables 齽龄鸲鳓鷾鹨鶆鸩龛齻鬐鳤鸃鸁鱆鹶麢鳜黶鴵龁龘龔齽鮚鳐鳍鹐鸃鸁鶹鵕龛鲟鵕鸃鸁龄鸲黝鲟鵕鸃鸁鵆鹶默鮕黾鸈鸇龄齔鴹鴜鴛鸔鸮鴘鼰

鵹齽 otherVar 鱆鹶默鵶鶧鷸鷵鸈鸇龄鵆鹶默麢麠龁 _raw_variables 齽龄鬎鸅齬鹶齻鳐鳍鸃鸁齅

鵀齔黿鮕黾鬐鳤鸃鸁齔龜鼰 $request->attributes->get('_raw_variables')

yunke.c:
  path: '/a/b/{area}'
  defaults:
    _controller: '\Drupal\yunke\Controller\HelloController::showme'
    _title: 'yunkec'
    otherVar:  'yunke'

补充内容

齸麗麼鯫魽騯鴹齸鴹鱽鱁鬼黹龛龘龔黓鷪齻鼏鼋鸱齯龄鹦齕騬鸇 drupal 龛騪鰾鼻齛 index.php 龄髤鶧鹗鶻麼鴜鴛鸔鸮鴘鼰

鸲鹦齕騨鴛魽髇騧鵴龛黪鸘鳅鳄鲛黤龄騦鮕鴹鲆鸊 index.php 鸇鲐黰齅

騣麗龘龔龓龏鱱鲐龛鷸鷵龄齬鵂騢骱麢麠齕鴠鹈鹄鼰

_route 鼰齸黶默麢麠龄鸨鳽鳅鳄鹍龛龘龔麨骁

_route_object 鼰龘龔黓鷪骁

_access_result 鼰鷴鷲鵢鵡鮑鯫黓鷪 Drupal\Core\Access\AccessResultAllowed

_route_params 鼰龘龔鷸鷵龄鮚鮙龘龔麨鴲麟麜麛鵕騹鶢鶟黛鯨龁齔黿鷚鷗麢鳓鴞骁

_raw_variables 鼰鳐鳍鹐鸃鸁龛鬐鳤鳩龄鹭鹪鶟鲛鷸鷵鸈鸇龓龏黮黪鲛骁

魦麗齻鴈鲣鷔龘鳢鸃鸁龛騠騟鳩默髬鱤龘龔鸨龛 pattern_outline 鸢鳌鳩龄鹪黝騞麨鵹騜龛鷲鵐齔鵢鵡齻鴈鴄騛龛鲣鷔鮙騠騟鳩齅麼 :path: '/hill/{area}/name' 鲣鷔騠騟鳩龛騚默鯙鳖龘龔鹶鸮鳅鳄龄齘齕鹳黮鹶鸮鲣鷔 area 龛騠騟鳩 , 鸮鳅鳄鸸齬齏鹪鳩龄鹶默龜黾鷼鷺鳩齅

騖鼰龘鳢 /a/b/{area}/ /a/b/{area} 鸴験龄麼鯫鳀鶈鷬龁龄齕 /a/b/{area} 鸅騏骁

騌鼰麼鯫骮齵龘龔鰞鸮鳅鳄龄鯖鼋龘龔麨黓髫齌鳱默髸鳔髬鱤龄齕龘龔麨龛鸢騉鰫鰪骒鰪骁

騇鼰龘龔 /friend/{area}/{name} 鸮鳅鳄齕鴠龘鳢鼰 friend/a/b/ friend/a/b 麼鯫鹭鹪鲣鷔鷼鷺鳩鸸鹶鸮鳅鳄髇騆龘鳢龄麼鯫鲣鷔鷼鷺鳩鸸鹧鹦齕鳅鳄齕鴠龘鳢鼰

friend/a/

friend/a

friend/

friend

騂鼰龁 yaml 鼻齛齽鮥鳩齽鲀龛騀鳙黹鴞齏鴐駿鹪鮁骱齸齵鲂黐龄鴈鸸鯙鳖 yaml 鸘鷏鴄騛龄駼骨鼽鯶鲣鳽鹶鹦駹駷龛鴄騛龄鸲齻龋鴤骥齋驁駶鹄駳駱龛鴄騛齅

駮鼰龁 drupal 齽龆鸿鸻鹈鹄齻齕驄鰌鰪鳙鸨鳽龛龄鹦齕鸅鵹鮽鯉麨龄鯖鼋鯉麨齻鴶鼋黮黪龛鼇鼅鯉麨麈麄鴔鷬齩 url_alias 鸨齽龄龁鵶鶧龘龔黃麿鶈默齌鸊鯉麨鸈鸇鸅鷎麽龛龘鳢龄鹳魉鵯龁龘龔鲣鷔麛龛 getRouteCollectionForRequest 鹤鹠齽鵶鶧龄鹗鶻麼鴠鼰

$path = $this->pathProcessor->processInbound($path, $request);

<?php
use Symfony\Component\Routing\Route;

$autoloader = require_once 'autoload.php';

$path="/new/{cms}/yunke_{type}/show";
$defaults=["cms"=>"drupal","type"=>"dev"];
$requirements=["cms"=>"\S+","type"=>"[a-zA-Z\d]+"];
$host="{sub}/baidu.com";
$r=new Route($path,$defaults,$requirements);
$r->setHost($host);
$r->setDefault("sub", "www");
$r->setRequirement("sub", "m|www");
$c=$r->compile();
print_r($c);
?>

本书共83小节:

评论 (写第一个评论)