3.4.3 Konfigurační třída
Navzdory potřeby konfigurovat jen několik málo věcí v CakePHP, někdy je užitečné mít vlastní konfigurační pravidla pro aplikaci. Z dřívějška můžete mít definovány konfigurační hodnoty v nějakých souborech. Toto Vás nutí vkládat konfigurační soubor pokaždé, když potřebujete tyto hodnoty.
Nová třída Configure může být použita k ukládání a přijímání aplikačních nebo runtimeových specifických hodnot. Buďte opatrní, tato třída Vám dovolí uložit cokoliv a potom to využít v jiné části kódu : toto zajisté svádí k porušení MVC vzorů. Hlavním cílem třídy Configure je udržovat centralizovaně proměnné, které mohou být sdíleny mezi mnoha objekty. Nezapomeň pracovat podle pravidla "konvence nad konfigurací" a zvykneš si neničit MVC strukturou, kterou jsme vytvořili.
Tato třída je vytvořena jako singleton a její metody mohou být voláný kdekoliv ve tvé aplikaci - ve statickém kontextu.
<?php Configure::read('debug'); ?>
<?php Configure::read('debug'); ?>
3.4.3.1 Metody třídy Configure
3.4.3.1.1 write
write(string $key, mixed $value)
Použijte write() k uložení dat do konfigurace aplikace.
Configure::write('Company.name','Pizza, Inc.');
Configure::write('Company.slogan','Pizza for your body and soul');
Configure::write('Company.name','Pizza, Inc.');Configure::write('Company.slogan','Pizza for your body and soul');
Využívejte tečkové notace v $key parametru k organizování dat do logických skupin.
Příklad výše může být napsán i jedním příkazem:
Configure::write(
'Company',array('name'=>'Pizza, Inc.','slogan'=>'Pizza for your body and soul')
);
Configure::write('Company',array('name'=>'Pizza, Inc.','slogan'=>'Pizza for your body and soul'));
Můžeš také použít Configure::write('debug', 0) k přepínání z debug do produkčního módu za běhu. Toto je speciálně šikovné pro spolupráci AMF a SOAP, kde debugovací informace mohou být důvodem parsovacích problémů.
3.4.3.1.2 read
read(string $key = 'debug')
Využívá se ke čtení konfiguračních dat z aplikace. Defaultně je pro CakePhp důležitá informace o debug hodnotě. Pokud je klíč nalezen, data jsou vrácena. Použijeme-li náš příklad s write() výše, můžeme načíst data zpět:
Configure::read('Company.name'); // vrací: 'Pizza, Inc.'
Configure::read('Company.slogan'); // vrací: 'Pizza for your body and soul'
Configure::read('Company'); //vrací: array('name' => 'Pizza, Inc.', 'slogan' => 'Pizza for your body and soul');
Configure::read('Company.name'); // vrací: 'Pizza, Inc.'Configure::read('Company.slogan'); // vrací: 'Pizza for your body and soul'Configure::read('Company'); //vrací: array('name' => 'Pizza, Inc.', 'slogan' => 'Pizza for your body and soul');
3.4.3.1.3 delete
delete(string $key)
Používá se k výmazu informace z aplikační konfigurace.
Configure::delete('Company.name');
Configure::delete('Company.name');
3.4.3.1.4 load
load(string $path)
Tato metoda načte konfigurační informace ze specifického souboru. Tento by měl být umístěn v /app/config s příponou php.
// /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');
?>
// /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';?><?phpConfigure::load('messages');Configure::read('Company.name');?>
Každý konfigurační pár klíč-hodnota musí být v souboru uložený v poli $config. Všechny ostatní proměnné v souboru budou funkcí load() ignorovány.
3.4.3.1.5 version
version()
Vrací verzi CakePHP pro aktuální aplikaci.
3.4.3.2 CakePHP Core Configuration Variables
The Configure class is used to manage a set of core CakePHP configuration variables. These variables can be found in app/config/core.php. Below is a description of each variable and how it affects your CakePHP application.
| Configure Variable | Description |
|---|---|
| debug |
Changes CakePHP debugging output. 0 = Production mode. No output. 1 = Show errors and warnings. 2 = Show errors, warnings, and SQL. 3 = Show errors, warnings, SQL, and complete controller dump. |
| App.baseUrl | Un-comment this definition if you don’t plan to use Apache’s mod_rewrite with CakePHP. Don’t forget to remove your .htaccess files too. |
| Routing.admin | Un-comment this definition if you’d like to take advantage of CakePHP admin routes. Set this variable to the name of the admin route you’d like to use. More on this later. |
| Cache.disable | When set to true, caching is disabled site-wide. |
| Cache.check | If set to true, enables view caching. Enabling is still needed in the controllers, but this variable enables the detection of those settings. |
| Session.save |
Tells CakePHP which session storage mechanism to use. php = Use the default PHP session storage. cake = Store session data in /app/tmp database = store session data in a database table. Make sure to set up the table using the SQL file located at /app/config/sql/sessions.sql. |
| Session.table | The name of the table (not including any prefix) that stores session information. |
| Session.database | The name of the database that stores session information. |
| Session.cookie | The name of the cookie used to track sessions. |
| Session.timeout | Base session timeout in seconds. Actual value depends on Security.level. |
| Session.start | Automatically starts sessions when set to true. |
| Session.checkAgent | When set to false, CakePHP sessions will not check to ensure the user agent does not change between requests. |
| Security.level |
The level of CakePHP security. The session timeout time defined in 'Session.timeout' is multiplied according to the settings here. Valid values: 'high' = x 10 'medium' = x 100 'low' = x 300 'high' and 'medium' also enable session.referer_check |
| Security.salt | A random string used in security hashing. |
| Acl.classname, Acl.database | Constants used for CakePHP’s Access Control List functionality. See the Access Control Lists chapter for more information. |
Cache configuration is also found in core.php — We’ll be covering that later on, so stay tuned.
The Configure class can be used to read and write core configuration settings on the fly. This can be especially handy if you want to turn the debug setting on for a limited section of logic in your application, for instance.
3.4.3.3 Configuration Constants
While most configuration options are handled by Configure, there are a few constants that CakePHP uses during runtime.
| Constant | Description |
|---|---|
| LOG_ERROR | Error constant. Used for differentiating error logging and debugging. Currently PHP supports LOG_DEBUG. |
