ComposerでCakePHPをインストールする方法です。
公式の情報にない手順が多かったので、いろいろ大変でした。
Composerのインストール
まずComposerをインストールしておいてください。
CakePHPのインストール
CakePHPをインストールしたい場所にcomposer.jsonファイルを作成します。
下記はCakePHPとDebugKitをインストールする設定です。
{
"name": "example-app",
"repositories": [
{
"type": "pear",
"url": "http://pear.cakephp.org"
}
],
"require": {
"cakephp/cakephp": "2.5.2",
"cakephp/debug_kit": "2.2.3"
},
"config": {
"vendor-dir": "Vendor/"
}
}
Windowsならコマンドプロンプトで、Macならターミナルでcomposer.jsonファイルを作成した場所に移動し、 “composer install” を実行します。
>composer install
Loading composer repositories with package information
Initializing PEAR repository http://pear.cakephp.org
Installing dependencies (including require-dev)
- Installing composer/installers (v1.0.15)
Loading from cache
- Installing cakephp/cakephp (2.5.2)
Loading from cache
- Installing cakephp/debug_kit (2.2.3)
Loading from cache
Writing lock file
Generating autoload files
Bakeでプロジェクトを作成
Windowsでは “Vendor\bin\cake.bat” ファイルを修正しないと実行できないので、下記のように”bash” を “cmd /C” へ修正します。Macでは必要ありません。
@ECHO OFF SET BIN_TARGET=%~dp0/../cakephp/cakephp/lib/Cake/Console/cake cmd /C "%BIN_TARGET%" %*
コマンドプロンプトで “Vendor\bin\cake bake project sample1” を実行します。
sample1プロジェクトが作成されました。
>Vendor\bin\cake bake project sample1 Welcome to CakePHP v2.5.2 Console --------------------------------------------------------------- App : CakePHP Path: D:\pleiades\e4.4-php\xampp\htdocs\CakePHP\ --------------------------------------------------------------- Skel Directory: D:\pleiades\e4.4-php\xampp\htdocs\CakePHP\Vendor\cakephp\cakephp\lib\Cake\Console\Templates\skel Will be copied to: sample1 --------------------------------------------------------------- Look okay? (y/n/q) [y] > y --------------------------------------------------------------- Created: sample1 in sample1 --------------------------------------------------------------- * Random hash key created for 'Security.salt' * Random seed created for 'Security.cipherSeed' * Cache prefix set * app/Console/cake.php path set. CakePHP is not on your `include_path`, CAKE_CORE_INCLUDE_PATH will be hard coded. You can fix this by adding CakePHP to your `include_path`. * CAKE_CORE_INCLUDE_PATH set to D:\pleiades\e4.4-php\xampp\htdocs\CakePHP\Vendor\cakephp\cakephp\lib in webroot/index.php * CAKE_CORE_INCLUDE_PATH set to D:\pleiades\e4.4-php\xampp\htdocs\CakePHP\Vendor\cakephp\cakephp\lib in webroot/test.php * Remember to check these values after moving to production server Project baked successfully!
絶対パスを相対パスへ修正
絶対パスで設定されている部分を相対パスに修正します。
if (function_exists('ini_set')) {
$root = dirname(dirname(dirname(__FILE__)));
// the following line differs from its sibling
// /app/Console/cake.php
ini_set('include_path', $root . PATH_SEPARATOR . $root . $ds . 'Vendor' . $ds . 'cakephp' . $ds . 'cakephp' . $ds . 'lib' . PATH_SEPARATOR . ini_get('include_path'));
}
/**
* The absolute path to the "cake" directory, WITHOUT a trailing DS.
*
* Un-comment this line to specify a fixed path to CakePHP.
* This should point at the directory containing `Cake`.
*
* For ease of development CakePHP uses PHP's include_path. If you
* cannot modify your include_path set this value.
*
* Leaving this constant undefined will result in it being defined in Cake/bootstrap.php
*
* The following line differs from its sibling
* /app/webroot/index.php
*/
define('CAKE_CORE_INCLUDE_PATH', ROOT . DS . 'Vendor' . DS . 'cakephp' . DS . 'cakephp' . DS . 'lib');
/**
* The absolute path to the "Cake" directory, WITHOUT a trailing DS.
*
* For ease of development CakePHP uses PHP's include_path. If you
* need to cannot modify your include_path, you can set this path.
*
* Leaving this constant undefined will result in it being defined in Cake/bootstrap.php
*
* The following line differs from its sibling
* /app/webroot/test.php
*/
define('CAKE_CORE_INCLUDE_PATH', ROOT . DS . 'Vendor' . DS . 'cakephp' . DS . 'cakephp' . DS . 'lib');
プラグインの読み込み部分を修正
Composerでインストールしたプラグインが読み込まれるように修正します。
ここは公式の内容を参考にしています。
CakeLog::config('error', array(
'engine' => 'File',
'types' => array('warning', 'error', 'critical', 'alert', 'emergency'),
'file' => 'error',
));
// Pluginディレクトリ指定を相対パスで
App::build(array('Plugin' => array(ROOT . DS . 'Plugin' . DS)));
// composerのautoloadを読み込み
require ROOT . DS . 'Vendor' . DS . 'autoload.php';
// CakePHPのオートローダーをいったん削除し、composerより先に評価されるように先頭に追加する
// https://github.com/composer/composer/commit/c80cb76b9b5082ecc3e5b53b1050f76bb27b127b を参照
spl_autoload_unregister(array('App', 'load'));
spl_autoload_register(array('App', 'load'), true, true);
CakePlugin::loadAll();
データベース設定
適当にデータベースを作成します。
create database cakephp_db default charset utf8
作成したデータベースに合わせて “Config\database.php.default” を元に “Config\database.php” を作成します。
class DATABASE_CONFIG {
public $default = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'user',
'password' => 'pass',
'database' => 'cakephp_db',
'prefix' => '',
//'encoding' => 'utf8',
);
public $test = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'user',
'password' => 'password',
'database' => 'test_database_name',
'prefix' => '',
//'encoding' => 'utf8',
);
}
動作確認
動作確認できました。



コメント