CakePHPで独自定数を定義する

このエントリーをはてなブックマークに追加
はてなブックマーク - CakePHPで独自定数を定義する
Share on Facebook

defineで定義するパターンと、連想配列で定義するパターンがあります。基本的に後者の方が便利です。

defineで定義する方法

1. /app/config/bootstrap.php に下記を追加する

// /app/config/bootstrap.php
<?php
config(‘const’);
?>

2. /app/config/const.php に定義したい定数を追加する

// /app/config/const.php
define(’USO’,'800’);

これで全ファイルから定数USOを呼び出せるようになっています。

連想配列で定義する方法

1. app/config/ 配下に、任意のコンフィグファイルを作成します。ここでは hige.php とします。

// app/config/hige.php
<?php
$config["Band1"] = array(
    "name"  => "Thelonious Monk Quartet",
    "piano" => "Thelonious Monk",
    "sax"   => "Charlie Rouse",
    "bass"  => "Larry Gales",
    "drums" => "Ben Riley",
);

$config["Band2"] = array(
    "name"      => "Miles Davis Quintet",
    "trumpet"   => "Miles Davis",
    "sax"       => "John Coltrane",
    "piano"     => "Wynton Kelly",
    "bass"      => "Paul Chambers",
    "drums"     => "Jimmy Cobb",
);

?>

2. app/config/bootstrap.php に下の行を追加します。

Configure::load("hige");

3. これで任意のスクリプトから設定した値を読み込めるようになりました。例えばコンソールに出力する場合、下記のようになります。

// app/vendors/shells/moja.php
<?php
    // エントリーポイント
    function main() {
        $this->out(Configure::read("Band1"));
        $this->out(Configure::read("Band1.name"));
        
        $this->hr();
        
        $this->out(Configure::read("Band2"));
        $this->out(Configure::read("Band2.name"));
    }
}
?>

出力結果

Thelonious Monk Quartet
Thelonious Monk
Charlie Rouse
Larry Gales
Ben Riley
Thelonious Monk Quartet
---------------------------------------------------------------
Miles Davis Quintet
Miles Davis
John Coltrane
Wynton Kelly
Paul Chambers
Jimmy Cobb
Miles Davis Quintet

参考

Leave a Comment


NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>