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'

本书共88小节:

评论 (写第一个评论)