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'

本书共63小节:

评论 (写第一个评论)