3.4.3.1 Metodi della classe Configure

write(string $key, mixed $value)

Si usa il metodo write() per salvare i dati nella classe Configure

<?php
 
Configure::write('Company.name','Pizza, Inc.');
Configure::write('Company.slogan','Pizza for your body and soul');
 
?>
  1. <?php
  2. Configure::write('Company.name','Pizza, Inc.');
  3. Configure::write('Company.slogan','Pizza for your body and soul');
  4. ?>

Osserva che si utilizza la notazione puntata nel parametro $key. Puoi usare questa notazione per organizzare i dati in gruppi logici.

L'esempio può essere riscritto in una sola riga:

<?php
 
Configure::write(
    'Company',array('name'=>'Pizza, Inc.','slogan'=>'Pizza for your body and soul')
);
 
?>
  1. <?php
  2. Configure::write(
  3. 'Company',array('name'=>'Pizza, Inc.','slogan'=>'Pizza for your body and soul')
  4. );
  5. ?>

Puoi usare Configure::write('debug', $int) per passare al volo tra l'ambiente di produzione e quello di debug. Questa caratteristica è particolarmente utile per le operazioni AMF e SOAP, dove le informazioni di debug possono causare errori.

read(string $key = ‘debug’)

Utilizzato per leggere un valore dalla configurazione dell'applicazione. Di default riporta l'impostazione di debug. Se fornisci una chiave, invece, riporta il suo valore. Utilizzando l'esempio di write() qui sopra, puoi leggere i valori così:

<?php
 
Configure::read('Company.name');    //da come risultato: 'Pizza, Inc.'
Configure::read('Company.slogan');  //da come risultato: 'Pizza for your body and soul'
 
Configure::read('Company');
 
//da come risultato:
 
array('name' => 'Pizza, Inc.', 'slogan' => 'Pizza for your body and soul');
 
?>
  1. <?php
  2. Configure::read('Company.name'); //da come risultato: 'Pizza, Inc.'
  3. Configure::read('Company.slogan'); //da come risultato: 'Pizza for your body and soul'
  4. Configure::read('Company');
  5. //da come risultato:
  6. array('name' => 'Pizza, Inc.', 'slogan' => 'Pizza for your body and soul');
  7. ?>
delete(string $key)

Usato per cancellare la variabile di configurazione.

<?php Configure::delete('Company.name'); ?>
  1. <?php Configure::delete('Company.name'); ?>
load(string $path)

Usato per caricare la configurazione da un file esterno.

/app/config/messages.php:
 
<?php
$config['Company']['name'] = 'Pizza, Inc.';
$config['Company']['slogan'] = 'Pizza for your body and soul';
$config['Company']['phone'] = '555-55-55';
?>
 
<?php
Configure::load('messages');
Configure::read('Company.name');
?>
  1. /app/config/messages.php:
  2. <?php
  3. $config['Company']['name'] = 'Pizza, Inc.';
  4. $config['Company']['slogan'] = 'Pizza for your body and soul';
  5. $config['Company']['phone'] = '555-55-55';
  6. ?>
  7. <?php
  8. Configure::load('messages');
  9. Configure::read('Company.name');
  10. ?>

Osserva che ogni parametro di configurazione è una coppia chiave/valore appartenente all'array $configure. Tutte le altre variabili presenti nel file saranno ignorate dalla funzione load().

version()

Restituisce la versione corrente di CakePHP.

3.4.3.1.1 write

write(string $key, mixed $value)

Use write() to store data in the application’s configuration.

Configure::write('Company.name','Pizza, Inc.');
Configure::write('Company.slogan','Pizza for your body and soul');
  1. Configure::write('Company.name','Pizza, Inc.');
  2. Configure::write('Company.slogan','Pizza for your body and soul');

the usage of dot notation in the $key parameter. You can use this notation to organize your configuration into logical groups.

The above example could also be written in a single call:

Configure::write(
    'Company',array('name'=>'Pizza, Inc.','slogan'=>'Pizza for your body and soul')
);
  1. Configure::write(
  2. 'Company',array('name'=>'Pizza, Inc.','slogan'=>'Pizza for your body and soul')
  3. );

You can use Configure::write('debug', $int) to switch between debug and production modes on the fly. This is especially handy for AMF or SOAP interactions where debugging information can cause parsing problems.

3.4.3.1.2 read

read(string $key = 'debug')

Used to read configuration data from the application. Defaults to CakePHP’s important debug value. If a key is supplied, the data is returned. Using our examples from write() above, we can read that data back:

Configure::read('Company.name');    //yields: 'Pizza, Inc.'
Configure::read('Company.slogan');  //yields: 'Pizza for your body and soul'
 
Configure::read('Company');
 
//yields: 
array('name' => 'Pizza, Inc.', 'slogan' => 'Pizza for your body and soul');
  1. Configure::read('Company.name'); //yields: 'Pizza, Inc.'
  2. Configure::read('Company.slogan'); //yields: 'Pizza for your body and soul'
  3. Configure::read('Company');
  4. //yields:
  5. array('name' => 'Pizza, Inc.', 'slogan' => 'Pizza for your body and soul');

3.4.3.1.3 delete

delete(string $key)

Used to delete information from the application’s configuration.

Configure::delete('Company.name');
  1. Configure::delete('Company.name');

3.4.3.1.4 load

load(string $path)

Use this method to load configuration information from a specific file.

// /app/config/messages.php:
<?php
$config['Company']['name'] = 'Pizza, Inc.';
$config['Company']['slogan'] = 'Pizza for your body and soul';
$config['Company']['phone'] = '555-55-55';
?>
 
<?php
Configure::load('messages');
Configure::read('Company.name');
?>
  1. // /app/config/messages.php:
  2. <?php
  3. $config['Company']['name'] = 'Pizza, Inc.';
  4. $config['Company']['slogan'] = 'Pizza for your body and soul';
  5. $config['Company']['phone'] = '555-55-55';
  6. ?>
  7. <?php
  8. Configure::load('messages');
  9. Configure::read('Company.name');
  10. ?>

Every configure key-value pair is represented in the file with the $config array. Any other variables in the file will be ignored by the load() function.

3.4.3.1.5 version

version()

Returns the CakePHP version for the current application.