18. 页面标题

龸龴龱龯龮龪 drupal8 龕龔龒龎龍龌龋龊龇龆龯龃龀龆龯齿齼齻龀齹齶齴齱齭齪齦齥齡齠齟齛龀龆龯齘齼齿齗齖

龊龇齓龆龯齑齏龒齎齴齍齊齇齃齂 html 鼷鼴龀鼰鼮鼪鼧鼦鼥鼡鼞龎鼝鼛鼘鼗鼔鼐鼍鼋鼊鼇齴鼅龀鼂鼀龆龯龌龋黼鼰鼛鼘鼗鼞黺黸黶鼦齖

鼥鼡黴黴黱黰鼰 Symfony 龃黠黝黛黚鼞龀龎鼝鼛鼘鼗鼍鼋龃龒黠黝黗黓龀鼂鼀黐黏黍黋黇黆黄龀龱黀麽黋黇麼麸鼗 MainContentViewSubscriber 黐麠麞齍齊齇齃龀麚麘麕齴鼇龃齇齃黠黝鼗黠黝麑麍齖麋麉龃麅龯黼黍麂鼰齍齊齇齃齂 html HtmlRenderer 黠黝鼗鼞龀鹽鼪黠黝鼗鹼鹸鹷龆龯龪鹶鼗齖

鹽黼龒龸龱龯龃黀麽龀鹴龇鹰鹰龆龯龒鹬鼀龌龋麍齟龃鹨

鹤鹣龎鼝鼛鼘鼗鼍鼋龃黠黝黗黓鹠鹟 #title 鹚鹗鹓龀龎鹨 $main_content['#title'] 龀鼂鼀黐鹇鹄鹃麕龃鹂鹼齶鹁龀龆龯龌龋鸽鸼齖

龎鼝鸻齓鹠鹟龀鸸鸷鹼龆龯龪鹶鼗黶鼦龌龋龀龆龯龪鹶鼗鸳鸱 id 齂鸰 title_resolver

鸬鹨 Drupal\Core\Controller\TitleResolver 鸢鸞鸛黗鼐鸘鼛鼘鼗龪鹶鼗鹨 controller_resolver 鸗鸖鸔鸒鸏鸌鸳鸱鹨 string_translation

龆龯龪鹶鸆鸅鸁龎鷽鷼黀麽鷹鷵鷱鹨

鹤鹣鷰鷮鷭鷩鷥鷤鼞龒鷣鷡鷠齓龆龯鼋鸷龀鷡鷠齇齃鷝鼛鼘鼗鸽鷙鷗鷕龀鷑鷍齂 _title_callback 龀齴鼋鸷龃鼧鼦鷝鼛鼘鼗鸽鷙鷗鷕齖

鷆鷄龆龯鼋鸷鷁鹸龃鶾鼝鹃鶻鶷鸏鸌龀鶶齻鸏鸌鶶齻鼰鼋鸷鼞黶鼦齖

龎鼝鸻齓鷡鷠鼋鸷鸸鷰鷮 _title 鹚鹗鹓龀龎鼝鷡鷠鹷鸸黶鼦鸏鸌鶳鶯鶮鸏鸌龕龔鶪鶦齍鹰鶥鶤龱龯鶠

鶝鷰龒鷣鷡鷠鸏鸌鶚鹴鶖 _title_context 鶐鶚鹴鶖鹼齱鸏鸌鷄鼡龃鶏鶍鶊鷤龀鶉龎 may 鹸鶅鸏鸌鸼鸰鶂鶁鸭鵿龒鸰鵽鵹鸭鵸鵷鷗鵳齂鵱齖

鵮鵬鸏鸌鵫黗龀黐齍齊鼞龃 _raw_variables 鹁鵩鵦鵫黗黗黓龀 _raw_variables 黴鹠鹟鵥鵤鸔鵣鵡龃鹇鵝鹁龀鹃鹠鵚鷭鷩鵘鵔鵐鵌龃鵣鵡齖

齏鶥鶝鷰鷭鷩龒鷣鵐鵌鵘鵔龆龯鵫黗龀龎鼝齓鸸鵈麚鵦鵫黗黗黓齖

齏鶥黐龆龯鵇鵫黗鵇鶚鹴鶖鵃鵁鵀鸏鸌龕龔黶鼦鸏鸌龀龆龯龌龋鶳鶯鸽鸼齖

齡鹣鴽鴹鶾鹨鼛鼘鼗鴵鷥 鴴龆龯鼋鸷 鴴鷭鷩龆龯鴵鷥齖

鴱齱鸏鸌龕龔龃鵫黗龪鴭鴬鹨

https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Component%21Ren…

鸏鸌龕龔鶚鹴鶖龪鴭鴬鹨

https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Language…

public function getTitle(Request $request, Route $route) {
    $route_title = NULL;
    // A dynamic title takes priority. Route::getDefault() returns NULL if the
    // named default is not set.  By testing the value directly, we also avoid
    // trying to use empty values.
    if ($callback = $route->getDefault('_title_callback')) {
      $callable = $this->controllerResolver->getControllerFromDefinition($callback);
      $arguments = $this->controllerResolver->getArguments($request, $callable);
      $route_title = call_user_func_array($callable, $arguments);
    }
    elseif ($title = $route->getDefault('_title')) {
      $options = array();
      if ($context = $route->getDefault('_title_context')) {
        $options['context'] = $context;
      }
      $args = array();
      if (($raw_parameters = $request->attributes->get('_raw_variables'))) {
        foreach ($raw_parameters->all() as $key => $value) {
          $args['@' . $key] = $value;  //@和%表示要进行转义的状态,见下文解释
          $args['%' . $key] = $value;
        }
      }
      if ($title_arguments = $route->getDefault('_title_arguments')) {
        $args = array_merge($args, (array) $title_arguments);
      }

      // Fall back to a static string from the route.
      $route_title = $this->t($title, $args, $options);
    }
    return $route_title;
  }

鷽鷼黀麽鷹鵀麍鷗鼪鸽鼮鴒鴎龃龆龯鷭鷩鷥鷤鹨

yunke.content:
  path: '/yunkehello'
  defaults:
    _controller: '\Drupal\yunke\Controller\HelloController::content'
    _title_callback: '\Drupal\yunke\Controller\HelloController::getTitle'
    _title: 'Hello @user ! time:@May'
    _title_context:'Long month name'
    _title_arguments: 
      @May: "May"
      @user: "yunke" 
  requirements:
    _permission: 'access content'

本书共78小节:

评论 (写第一个评论)