LoginSignup
33
32

More than 5 years have passed since last update.

【CakePHP】【Composer】CakePHPのインストール〜Composerの場合〜

Last updated at Posted at 2014-10-26

参考

CakePHP本サイト ----- 応用インストール PEARインストーラでCakePHPをインストール
http://book.cakephp.org/2.0/ja/installation/advanced-installation.html

Composerを使ってCakePHPをインストール

compoer.jsonファイルの作成

$ cd [project directory]        # プロジェクトを作成するディレクトリへ移動
$ touch composer.json           # composer.jsonファイルを作成
$ vim composer.json         # composer.jsonファイルへ下記を追記
{
    "name": "cakePHP",
    "repositories": [
        {
            "type": "pear",
            "url": "http://pear.cakephp.org"
        }
    ],
    "require": {
        "cakephp/cakephp": ">=2.4.9"
    },
    "config": {
        "vendor-dir": "Vendor/"
    }
}

CakePHP本体をインストール

$ composer install      # cakePHPの本体をインストール
Loading composer repositories with package information
Initializing PEAR repository http://pear.cakephp.org
Installing dependencies (including require-dev)
  - Installing cakephp/cakephp (2.5.5)
    Loading from cache

Writing lock file
Generating autoload files

CakePHPプロジェクトをbakeして作成

$ Vendor/bin/cake bake project app

Welcome to CakePHP v2.5.5 Console
---------------------------------------------------------------
App : cake_composer
Path: /var/www/html/cake_composer/  <----- この辺りのPATHは個々人の環境に依存します
---------------------------------------------------------------
Skel Directory: /var/www/html/cake_composer/Vendor/cakephp/cakephp/lib/Cake/Console/Templates/skel  <----- この辺りのPATHは個々人の環境に依存します
Will be copied to: /var/www/html/cake_composer/app  <----- この辺りのPATHは個々人の環境に依存します
---------------------------------------------------------------
Look okay? (y/n/q)
[y] > y
---------------------------------------------------------------
Created: app in /var/www/html/cake_composer/app <----- この辺りのPATHは個々人の環境に依存します
---------------------------------------------------------------
 * 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 /var/www/html/cake_composer/Vendor/cakephp/cakephp/lib in webroot/index.php
 * CAKE_CORE_INCLUDE_PATH set to /var/www/html/cake_composer/Vendor/cakephp/cakephp/lib in webroot/test.php
   * Remember to check these values after moving to production server
Project baked successfully!

プロジェクトの設定を変更

$ vim app/webroot/index.php # 既に設定されているCAKE_CORE_INCLUDE_PATHを下記のPATHの設定に置き換え
define(
    'CAKE_CORE_INCLUDE_PATH',
    ROOT . '/Vendor/cakephp/cakephp/lib'
);

database設定の追記

CakePHPのためのデータベースの準備

$ mysql -u root -p  # MySQLへログイン
Enter password: # パスワードを入力
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.1.73 Source distribution

Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> SHOW DATABASES;  # 現在存在するデータベースを表示
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
+--------------------+
2 rows in set (0.00 sec)


mysql> CREATE DATABASE `cake_composer`; # cakephp_tutorial用のデータベースを'cakephp_tutorial'という名前で作成
Query OK, 1 row affected (0.00 sec)

mysql> GRANT ALL ON `cake_composer`.* to ${USERNAME}@localhost IDENTIFIED BY '*********';   # cakephp_tutorialデータベースに関する全ての権限を{$USERNAME}のユーザに付与
Query OK, 0 rows affected (0.00 sec)

mysql> FLUSH PRIVILEGES;    # 上記権限をMySQLに反映
Query OK, 0 rows affected (0.00 sec)

mysql> exit;    # MySQLを閉じる

CakePHPのデータベース設定ファイルに作成したデータベース情報を登録

参考: http://book.cakephp.org/2.0/ja/tutorials-and-examples/blog/blog.html#id3

$ cd /var/www/html/cakephp_tutorial
$ cp app/Config/database.php.default app/Config/database.php    # データベース設定ファイルの雛形ファイルを実際に利用するファイルとしてコピー

・app/Config/database.phpに先ほど作成したデータベースのユーザ・パスワードの設定を反映
・文字コードの部分のコメントアウトを削除して、'utf-8'の設定を有効にしておきましょう

Cakephp_tutorial3.png

DebugKitプラグインの導入と設定

DebugKitプラグインをインストール

$ cd app                # プロジェクトディレクトリへ移動

$ touch composer.json       # composer.jsonファイルを作成

$ vim composer.json     # composer.jsonに書きを追記(オフィシャルCakeの設定)
{
    "name": "cakephp/cakephp",
    "description": "The CakePHP framework",
    "type": "library",
    "keywords": ["framework"],
    "homepage": "http://cakephp.org",
    "license": "MIT",
    "authors": [
        {
            "name": "CakePHP Community",
            "homepage": "https://github.com/cakephp/cakephp/graphs/contributors"
        }
    ],
    "support": {
        "issues": "https://github.com/cakephp/cakephp/issues",
        "forum": "http://stackoverflow.com/tags/cakephp",
        "irc": "irc://irc.freenode.org/cakephp",
        "source": "https://github.com/cakephp/cakephp"
    },
    "config": {
        "vendor-dir": "Vendor/"
    },
    "require": {
        "php": ">=5.2.8",
        "ext-mcrypt": "*"
    },
    "require-dev": {
        "phpunit/phpunit": "3.7.*",
        "cakephp/debug_kit" : "2.2.*"
    },
    "bin": [
        "lib/Cake/Console/cake"
    ]
}

$ composer install  # プロジェクト用のプラグインをインストール

$ vim app/Config/bootstrap.php  # ライブラリの読み込みのPATHを変更するための下記を追記
# composerのautoloadを読み込み
require_once ROOT . DS . 'Vendor/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);

DebutKitプラグインを使えるように設定を変更

$ vim Config/bootstrap.php  # Pluginの読み込みを追記
+ CakePlugin::load('DebugKit');

CakePHP_tutorial6.png

DebugKitのツールバーを導入

Debugに必要な情報をリッチに表示するためのツールバーの設定

$ vim app/Controller/AppController.php
+ public $components = array('DebugKit.Toolbar');

CakePHP_tutorial7.png

Debugモードを1にする

CakePHPのデフォルトのDebugモード設定はSQLのダンプとかもしてくれるが、DebugKitの機能とカニバってくるために、ここでDebugモードの設定を1に変更

$ vim app/Config/core.php
- Configure::write('debug', 2);
+ //Configure::write('debug', 2);
+ Configure::write('debug', 1);

CakePHP_tutorial8.png

デフォルトのSQLデバッグ表示をコメントアウト

CakePHPのデフォルトのSQLダンプを表示するView側の要素をコメントアウト

$ vim app/View/Layouts/default.ctp
- <?php echo $this->element('sql_dump'); ?>
+ <?php // echo $this->element('sql_dump'); ?>

CakePHP_tutorial9.png

基本設定が完了

以下のようにオールグリーンになっていればOK!!

CakePHP_tutorial11.png

33
32
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
33
32