9.6 配置API

1龺龶龵龳龯

// Immutable Config (Read Only)
$config = \Drupal::config('system.performance');
// Mutable Config (Read / Write)
$config = \Drupal::service('config.factory')->getEditable('system.performance');

齊齈齄齂龶鼾齊鼺齄鼸鼴鼲龶鼰鼯

龳龯龶龵齈鼫鼧鼤get鼸鼴鼯鼠鼜

$config = \Drupal::config('system.maintenance');
$message = $config->get('message');

鼛鼲鼚鼖鼔鼐鼎鼰鼴鼜

$message = \Drupal::config('system.maintenance')->get('message');

龶龵齈齄鼋鼈龳龯鼾鼧鼤.鼄鼃黿黼黺鼯鼠鼜

$enabled = \Drupal::config('system.performance')->get('cache.page.enabled');

2龺鼰黴龳龯

黱黰鼧鼤getEditable黬齄鼸鼴黨\Drupal\Core\Config\Config黧黣龵鼯 鼠鼜

\Drupal::service('config.factory')->getEditable('system.performance');

黟鼰黝齄龳龯

齈鼫鼧鼤set黛save鼸鼴黺黙黗龳龯鼯鼠鼜

$config = \Drupal::service('config.factory')->getEditable('system.performance');

// Set a scalar value.
$config->set('cache.page.enabled', 1);

// Set an array of values.
$page_cache_data = array('enabled' => 1, 'max_age' => 5);
$config->set('cache.page', $page_cache_data);

// Save your data to the file system.
$config->save();

鼛鼲鼚鼰黃鼐鼎鼜

\Drupal::service('config.factory')->getEditable('system.performance')->set('cache.page.enabled', 1)->save();

黟鼰黀麽龳龯

鼠麺麹黟麷黀麽麶龳龯麲麱鼾鼲鼚鼧鼤setData鼸鼴鼾麮麭鼠麫:

// Set all values.
\Drupal::service('config.factory')->getEditable('system.performance')->setData(array(
    'cache' => array(
      'page' => array(
        'enabled' => '0',
        'max_age' => '0',
      ),
    ),
    'preprocess' => array(
      'css' => '0',
      'js' => '0',
    ),
    'response' => array(
      'gzip' => '0',
    ),
  ))
  ->save();

麡麠鼜setData麝麚麙鼖黙龳龯麲麱麖麕鼾麒麐黗鼖麶麏麍麊麇麅麂鹾鼯

3龺鹹鹶龳龯

鹹鹶齈齄黝鹳麶龳龯

麮麭鼠麫鼜

$config = \Drupal::service('config.factory')->getEditable('system.performance');
$config->clear('cache.page.max_age')->save();
$page_cache_data = $config->get('cache.page');

黬麏麍鹯鹭$page_cache_data鼾麝鹬鹨max_age鹧鹥鹣鹹鹶鹢鼯

鹹鹶黀麽龳龯

黱黰鼧鼤delete鼸鼴鼾麮麭鼠麫鼜

\Drupal::service('config.factory')->getEditable('system.performance')->delete();

4龺鹝鹛鹗鹕

鼠麺麹鹑齈齄龳龯鹐鹍鹊鹉齄鹅鹄鼾鹂鸿鼲鸾麹鼰鹉齄save鼾鼠麫鼜

\Drupal::service('config.factory')->getEditable('foo.bar')->set('foo', 'foo')->save();
\Drupal::service('config.factory')->getEditable('foo.bar')->set('bar', 'bar')->save();

黬鸼麮麭鸻鸸鸶鸳麶鼾麇鸯鸬麹黺鼰鼜

$config = \Drupal::service('config.factory')->getEditable('foo.bar');
$config
  ->set('foo', 'foo')
  ->set('bar', 'bar')
  ->save();

本书共79小节:

评论 (写第一个评论)