プログラミングしたり。

HTML/CSS/JavaScript/PHPを中心にWebサイト作ったり。Webサービス作ったり。

CakePHPを導入する

CakePHPの導入方法について、記述します。 事前準備として、サーバ環境はVagrantで構築して、http://dev.cakephp-test.com/というURLを与えています。 これをhttp://dev.cakephp-test.com/tutorial/として、アクセスしてCakPHPを使用していきます。

CakePHPをダウンロードする

公式サイトからCakePHPをダウンロードして、作業ディレクトリにファイル一式コピーします。 今回は、tutorialディレクトリを作成して、その中にコピーしました。

ターミナルにて、作業ディレクトリに移動して下記コマンドを入力します。 tmpディレクトリのアクセス権を変更します。

1
2
cd tutorial
chmod -R 777 app/tmp

セキュリティ設定を変更する

この時点で、http://dev.cakephp-test.com/tutorial/にアクセスすると、色々とメッセージが出ていると思います。

例えば…

Notice (1024): Please change the value of ‘Security.salt’ in APP/Config/core.php to a salt value specific to your application. [CORE/Cake/Utility/Debugger.php, line 846] Notice (1024): Please change the value of ‘Security.cipherSeed’ in APP/Config/core.php to a numeric (digits only) seed value specific to your application. [CORE/Cake/Utility/Debugger.php, line 850]

上記メッセージにあるようにapp/config/core.phpを開いて、Security.salt,Security.cipherSeedの適当な好きな文字列に変更しましょう。

DBの設定を行う

app/config/database.php.defaultファイルをコピーして、database.phpを作成します。

1
vi app/Config/database.php

下記部分を変更します。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public $default = array(
  'datasource' => 'Database/Mysql',
  'persistent' => false,
  'host' => 'localhost',
  'login' => 'dbuser',//DBのユーザ名
  'password' => 'dbuser',//パスワード
  'database' => 'tutorial',//使用するDB名に変更
  'prefix' => '',
  //'encoding' => 'utf8',
);

public $test = array(
  'datasource' => 'Database/Mysql',
  'persistent' => false,
  'host' => 'localhost',
  'login' => 'dbuser',//DBのユーザ名(テスト用)
  'password' => 'dbuser',//パスワード(テスト用)
  'database' => 'tutorial_test',//使用するDB名に変更(テスト用)
  'prefix' => '',
  //'encoding' => 'utf8',
);

次にDBを作成します。

1
2
3
4
5
6
7
8
9
vagrant ssh //仮想サーバにログイン
mysql -u root -p //パスワードは初期だとなし
create database tutorial
create database tutorial_test
grant all on tutorial.* to dbuser@localhost identified by 'dbuser';
exit

mysql -u dbuser -p //パスワードはdbuser
show databases; //tutorialが見えればOK

Debug_kitをダウンロードする

下記コマンドを入力します。

1
2
cd app/Plugin
git clone git@github.com:cakephp/debug_kit.git

app/Config/bootstrap.phpCakePlugin::load('DebugKit');という記述を追加します。

また、app/Controller/AppController.phpに下記内容を追加します。

1
2
3
class AppController extends Controller {
    public $components = array('DebugKit.Toolbar'); // この行の追加
}

以上でCakePHPを使用する準備が整いました。 あとは、[ブログチュートリアル]な(http://book.cakephp.org/2.0/ja/getting-started.html)どを参考に勉強していくのがよいですね!

なお、この時点でhttp://dev.cakephp-test.com/tutorial/にアクセスしたときに下記のようなメッセージが表示されている場合は、以下のことを行ってみてください。

メッセージ:URL rewriting is not properly configured on your server. 1) Help me configure it 2) I don’t / can’t use URL rewriting

httpd.confを編集します。

1
2
3
4
5
6
7
8
9
sudo vi /etc/httpd/conf/httpd.conf

//下記部分を編集します。
<Directory />
    Options FollowSymLinks
    AllowOverride All //ここ
#    Order deny,allow
#    Deny from all
</Directory>

次に、作業ディレクトリ(今回の場合、tutorial)に.htaccessファイルを作成します。 そして、下記内容を記述します。

1
2
3
4
5
<IfModule mod_rewrite.c>
   RewriteEngine on
   RewriteRule    ^$ app/webroot/    [L]
   RewriteRule    (.*) app/webroot/$1 [L]
</IfModule>

Comments