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\S+)/yunke_(?P[a-zA-Z\d]+)/show$#s

鶌鳟鸅鼽 $host="{sub}/baidu.com"; 鲘鲔鳜齠龌 $requirements=["sub"=>"m|www"];

鰧鲺鶤鰺鬶鶜龣鸼鸺鸥鼊黏鸅鼽 #^(?Pm|www)/baidu\.com$#si

鲘鲔鳜齠鼒龌龒鼂鴚龎 requirements 鳓龎鲟鼂龣鸼鸺齋鹶鬊鬇鸼鸺鹥鸭鹶黷鸚 (?P\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);
?>

本书共63小节:

评论 (写第一个评论)