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

评论 (写第一个评论)