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小节:

评论 (写第一个评论)