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'

本书共71小节:

评论 (写第一个评论)