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);
?>

本书共71小节:

评论 (写第一个评论)