Configurationクラス
CakePHPで設定しなければいけないことはそれほど多くありませんが、アプリケーションの中に独自のルールを設定できるようにしておくと、便利なことがあります。これまではカスタム設定に関する変数や定数をいくつかのファイルの中に定義していたかもしれませんが、そうすると、値が必要になるたびに、毎回、設定ファイルをincludeしなくてはならなくなります。
CakePHPの新しいConfigureクラスを使うと、アプリケーション、またその実行時に必要な特定の値の保存と取り出しに使うことができます。このクラスの中には何でも保存でき、コード内のあらゆる場所で使用できるので、CakePHPのMVCパターンを崩してしまう誘惑には注意しましょう。Configureクラスの主要な目標は、各変数を集中保管して、各オブジェクトから使用できるようにすることです。“設定より規約”(convention over configuration)を遵守するようにして、MVC構造を壊してしまうことがないようにしましょう。
このクラスは singleton として動作し、メソッドは静的なコンテキストとして、アプリケーション内のどこからでも呼ぶことができます。
<?php Configure::read('debug'); ?>
<?php Configure::read('debug'); ?>
Configureのメソッド
write
write(string $key, mixed $value)
write(string $key, mixed $value)
write()を使って、アプリケーションの設定データを保存します。
Configure::write('Company.name','株式会社ピザ');
Configure::write('Company.slogan','体と心にピザを');
Configure::write('Company.name','株式会社ピザ');Configure::write('Company.slogan','体と心にピザを');
$keyのパラメータとして、ドットを使っていることに注意してください。このドットを使って、論理グループごとに設定を整理することができます。
Configure::write(
'Company',array('name'=>'株式会社ピザ','slogan'=>'体と心にピザを')
);
Configure::write('Company',array('name'=>'株式会社ピザ','slogan'=>'体と心にピザを'));
Configure::write(‘debug’, $int)を使って、デバッグと運用モードをすぐに切り替えることができます。AMFやSOAPなどとの連携の際、デバッグ情報のためにパースに問題が生じるような場合に特に便利に使うことができます。
read
read(string $key = 'debug')
read(string $key = 'debug')
アプリケーションから、環境設定データを読み込むために使います。デフォルトでは、CakePHPの重要なデバッグ値が設定されています。keyが指定されると、そのデータが返されます。上のwrite()の例を使うと、返される次のようなデータを取得できます。
Configure::read('Company.name'); //出力は: '株式会社ピザ'
Configure::read('Company.slogan'); //出力は: '体と心にピザを'
Configure::read('Company');
//出力は:
array('name' => '株式会社ピザ', 'slogan' => '体と心にピザを');
Configure::read('Company.name'); //出力は: '株式会社ピザ'Configure::read('Company.slogan'); //出力は: '体と心にピザを'Configure::read('Company');//出力は:array('name' => '株式会社ピザ', 'slogan' => '体と心にピザを');
delete
delete(string $key)
delete(string $key)
アプリケーションの環境設定から情報を削除するのに使います。
Configure::delete('Company.name');
Configure::delete('Company.name');
load
load(string $path)
load(string $path)
指定したファイルから環境設定をloadするためにこのメソッドを使ってください。
// /app/config/messages.php:
<?php
$config['Company']['name'] = '株式会社ピザ';
$config['Company']['slogan'] = '体と心にピザを';
$config['Company']['phone'] = '555-55-55';
?>
<?php
Configure::load('messages');
Configure::read('Company.name');
?>
// /app/config/messages.php:<?php$config['Company']['name'] = '株式会社ピザ';$config['Company']['slogan'] = '体と心にピザを';$config['Company']['phone'] = '555-55-55';?><?phpConfigure::load('messages');Configure::read('Company.name');?>
ファイルの中では、$config配列のキーと配列のペアで設定されていることに注意してください。ファイル内のその他の変数は、load()では無視されます。
version
version()
version()
現在のアプリケーションのCakePHPのバージョンを返します。
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’s usage 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 |
| 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. |
Note: 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.
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. |

login to add a comment