LoginSignup
38
35

More than 5 years have passed since last update.

CakePHP2をComposerでサクッとセットアップする

Last updated at Posted at 2015-05-02

CakePHP3はcomposerでのセットアップがデフォルトだが、cakephp2はそういうアナウンスがされておらず、応用インストールという扱いになっている。
http://book.cakephp.org/2.0/ja/installation/advanced-installation.html

CakePHP3が出たとはいっても、CakePHP2もまだまだ現役。今後仕事でまた本格的に使いはじめるのでまとめてみた。cakephp.orgから落とせるzipファイルとほぼ同じディレクトリ構成を目指すよ。

アプリのディレクトリを作成して作業開始。

$ mkdir cakephpapp/ # アプリのディレクトリ作成

composer.jsonの記述

以下の様なcomposer.jsonを作成する。公式bookに記載されていたものをベースに様々追加。

公式book:http://book.cakephp.org/2.0/ja/installation/advanced-installation.html

cakephpapp/composer.json
{
    "name": "example-app",
    "repositories": [
        {
            "type": "pear",
            "url": "http://pear.cakephp.org"
        }
    ],
    "require": {
        "cakephp/cakephp": ">=2.6.4,<3.0.0"
    },
    "require-dev": {
        "phpunit/phpunit": "3.7.37",
        "cakephp/debug_kit" : ">=2.2.4,<3.0.0"
    },
    "config": {
        "vendor-dir": "vendors/"
    },
    "extra": {
        "installer-paths": {
            "./plugins/{$name}/": [
                "cakephp/debug_kit"
            ]
        }
    }
}

現在CakePHP2系の最新版が2.6.4なので、requireにてバージョン2.6.4以上3.0.0未満を指定。

CakePHP2はPHPUnit4系には対応していないため、PHPUnitは3.7.37(最新版)以上4.0.0未満を指定。debug_kitも同様に2.2.4以上3未満を指定。

また/vendors/pluginsはgit管理に入れて/app/Vendor/app/Pluginはgit管理する構成にしたかったのでインストールされるディレクトリを調整した。

bakeする

cakephpapp/appというappディレクトリの中に主なソースコードを生成する。

$ vendors/bin/cake bake project app

Welcome to CakePHP v2.6.4 Console
---------------------------------------------------------------
App : testcakephp2
Path: /Users/degawaikuo/workspace/testcakephp2/
---------------------------------------------------------------
Skel Directory: /Users/degawaikuo/workspace/testcakephp2/vendors/cakephp/cakephp/lib/Cake/Console/Templates/skel
Will be copied to: /Users/degawaikuo/workspace/testcakephp2/app
---------------------------------------------------------------
Look okay? (y/n/q)
[y] > y
---------------------------------------------------------------
Created: app in /Users/degawaikuo/workspace/testcakephp2/app
---------------------------------------------------------------
 * 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 /Users/degawaikuo/workspace/testcakephp2/vendors/cakephp/cakephp/lib in webroot/index.php
 * CAKE_CORE_INCLUDE_PATH set to /Users/degawaikuo/workspace/testcakephp2/vendors/cakephp/cakephp/lib in webroot/test.php
   * Remember to check these values after moving to production server
Project baked successfully!

これでいっぱいコードができたはず。
PHPのビルトインサーバーで動作確認。

$ cd app/webroot
$ php -S localhost:8989

ブラウザでlocalhost:8989にアクセスすると以下の様な画面になるはず。

Screen Shot 2015-05-02 at 12.40.35.png

DebugKitの有効化

/app/Config/bootstrap.phpに以下の1行を追記すればよし。さっきの画面の黄色が一つ緑になるはず。

/app/Config/bootstrap.php
CakePlugin::loadAll();

ブラウザでlocalhost:8989/test.phpを叩いて以下の様な画面が出たらOK。

Screen Shot 2015-05-02 at 12.47.18.png

データベースの設定

説明省略。これでオールグリーンになる。

ハードコーディングの解消

/app/webroot/index.phpやtest.phpをみるとディレクトリがめっちゃハードコーディングされていてデプロイさせられないので修正する。

index.phpとtest.phpの55行目あたり
// これを
// define('CAKE_CORE_INCLUDE_PATH',  DS . 'Users' . DS . 'username'. DS . 'testcakephp2' . DS . 'vendors' . DS . 'cakephp' . DS . 'cakephp' . DS . 'lib');

// こうする
define('CAKE_CORE_INCLUDE_PATH',  ROOT. DS . 'vendors' . DS . 'cakephp' . DS . 'cakephp' . DS . 'lib');

git init

.gitignore作る。

/.gitignore
/vendors
/plugins

/app/tmp/*
/app/Config/core.php
/app/Config/database.php

composer.lockはignoreせずちゃんとcommitに含めましょうね。
https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file

$ git init
$ git add .
$ git commit -m'initial commit'

以上

サクッとここまで作れるとプロトタイピングも早くなりますね。scaffoldもつかって即効でCRUDアプリを作るタイムアタックも今度やります。

その他参考資料

38
35
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
38
35