1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

CakePHP 2.7の導入方法

Last updated at Posted at 2015-11-18

CakePHP 2.7を導入する方法についてまとめてみます。

前提

  • Gitでソースコードを管理する。
  • DBとしてSQLite3を使用する。
  • jQuery,jQuery UI,Datepickerを使用する。
  • システムを日本標準時を基準に稼働させる。
  • 画像のアップロード処理に、UploadPackを使用する。
  • 実行環境のドキュメントルートが~{ユーザ名}/public_htmlにある。
  • 開発環境の{プロジェクトディレクトリ}/{開発用ディレクトリ}内に、開発用のファイルが置かれる。
  • 実行環境のPHPのバージョンが5.4.16よりも前のものである(5.4.16以降の場合には、最新のCakePHPが使用できます)。

開発環境の設定

前提

  • Git,SQLite3がインストールされている。

手順

  1. cd {プロジェクトディレクトリ}コマンドを実行し、カレントディレクトリをドキュメントルートに移動します。

  2. git clone --single-branch --branch 2.7 git://github.com/cakephp/cakephp.git {開発用ディレクトリ}コマンドを実行し、CakePHP 2.7をダウンロードします。

  3. cd {開発用ディレクトリ}コマンドを実行し、カレントディレクトリを移動します。

  4. git clone --single-branch --branch 2.2 git://github.com/cakephp/debug_kit.git app/Plugin/DebugKitコマンドを実行し、DebugKitを追加します。

  5. git clone --single-branch git://github.com/szajbus/uploadpack app/Plugin/UploadPackコマンドを実行し、UploadPackをダウンロードします。

  6. rm -rf .gitコマンドを実行し、Gitの設定ファイルが入ったディレクトリを削除します。

  7. rm -rf app/Plugin/DebugKit/.git*コマンドを実行し、Gitの設定ファイルを削除します。

  8. rm -rf app/Plugin/UploadPack/.git*コマンドを実行し、Gitの設定ファイルを削除します。

  9. rm app/Plugin/emptyコマンドを実行し、emptyファイルを削除します。

  10. mkdir app/webroot/uploadコマンドを実行し、uploadディレクトリを作成します。

  11. touch app/webroot/upload/emptyコマンドを実行し、emptyファイルを作成します。

  12. {プロジェクトディレクトリ}/{開発用ディレクトリ}/.gitignoreを次のように編集します。

     # User specific & automatically generated files #
     #################################################
     build
     dist
     tags
     *.mo
     *.diff
     *.err
     *.orig
     *.rej
     *.swo
     *.swp
     *.vi
     *.lock
     bin
     
     # OS generated files #
     ######################
     .DS_Store*
     ._*
     .Spotlight-V100
     .Trashes
     Icon?
     *.db
     *~
    
  13. {プロジェクトディレクトリ}/{開発用ディレクトリ}/app/Config/bootstrap.phpの末尾に、CakePlugin::loadAll();を追加します。

  14. {プロジェクトディレクトリ}/{開発用ディレクトリ}/app/View/Layouts/default.ctp{プロジェクトディレクトリ}/{開発用ディレクトリ}/app/View/Layouts/error.ctp<?php echo $this->element('sql_dump'); ?>を削除します。

  15. {プロジェクトディレクトリ}/{開発用ディレクトリ}/app/Controller/AppController.phpを次のように編集します。

     class AppController extends Controller {
     	//追加部分
     	public $components = array('DebugKit.Toolbar');
     }
    
  16. mkdir dbコマンドを実行し、SQLite用DBファイルを格納するディレクトリを作成します。

  17. sqlite3 db/{DBファイル名}コマンドを実行し、SQLiteを起動します。
    この際、DBファイル名を{ファイル名}.sqlite3といった形にすると、他のファイルと区別がついて良いでしょう。
    dbという拡張子にはしないことをおすすめします(後述のデプロイメントの設定をそのまま適用できなくなるため)。

  18. 何らかのコマンドを実行します。
    この際にテーブルを作成しても良いですが、そのつもりがなければ、とりあえず.tablesコマンドあたりを実行しておくと良いでしょう。

  19. .exitコマンドを入力し、SQLiteを終了します。

  20. cp app/Config/database.php.default app/Config/database.phpコマンドを実行し、{プロジェクトディレクトリ}/{開発用ディレクトリ}/app/Config/database.php.default{プロジェクトディレクトリ}/{開発用ディレクトリ}/app/Config/database.phpというファイル名で同じディレクトリ内にコピーします。

  21. {プロジェクトディレクトリ}/{開発用ディレクトリ}/app/Config/database.phpを次のように編集します。

     class DATABASE_CONFIG {
     
     	public $default = array(
     		//変更部分
     		'datasource' => 'Database/Sqlite',
     		'persistent' => false,
     		//変更部分
     		'database' => '{実行環境でのDBファイルの絶対パス}',
     		'prefix' => '',
     		//'encoding' => 'utf8',
     	);
     
     	public $test = array(
     		//変更部分
     		'datasource' => 'Database/Sqlite',
     		'persistent' => false,
     		//変更部分
     		'database' => '{実行環境でのDBファイルの絶対パス}',
     		'prefix' => '',
     		//'encoding' => 'utf8',
     	);
     
     }
    
  22. {プロジェクトディレクトリ}/{開発用ディレクトリ}/.htaccessを次のように編集します。

     <IfModule mod_rewrite.c>
     	RewriteEngine on
     	RewriteRule ^$ app/webroot/ [L]
     	RewriteRule (.*) app/webroot/$1 [L]
     	#追加部分
     	RewriteBase /{ドキュメントルート}/{サイト名}
     </IfModule>
    
  23. {プロジェクトディレクトリ}/{開発用ディレクトリ}/db/.htaccessを次のように作成します。

     order deny,allow
     deny from all
    
  24. {プロジェクトディレクトリ}/{開発用ディレクトリ}/app/webroot/.htaccessを次のように編集します。

     <IfModule mod_rewrite.c>
     	RewriteEngine On
     	RewriteCond %{REQUEST_FILENAME} !-d
     	RewriteCond %{REQUEST_FILENAME} !-f
     	RewriteRule ^ index.php [L]
     	#追加部分
     	RewriteBase /{ドキュメントルート}/{サイト名}/app/webroot
     </IfModule>
    
  25. {プロジェクトディレクトリ}/{開発用ディレクトリ}/app/Config/core.phpを次のように編集します。

  26. セキュリティ関連の値の設定

       /**
        * A random string used in security hashing methods.
        */
       	//変更部分
       	Configure::write('Security.salt', '{適当な文字列}');
       
       /**
        * A random numeric string (digits only) used to encrypt/decrypt strings.
        */
       	//変更部分
       	Configure::write('Security.cipherSeed', '{適当な数値}');
    
  27. タイムゾーンの設定

       /**
        * Uncomment this line and correct your server timezone to fix
        * any date & time related errors.
        */
       	//変更部分
       	date_default_timezone_set('Asia/Tokyo');
    
  28. ThemeRoller | jQuery UIの左のボックスのにあるGalleryをクリックし、お好みのテーマを選びます。

  29. {プロジェクトディレクトリ}/{開発用ディレクトリ}/app/View/Layouts/default.ctpを次のように編集します。

     //追加部分
     echo $this->Html->css('//code.jquery.com/ui/1.11.4/themes/{前の手順で選んだテーマ名の空白をハイフンで置換した名称}/jquery-ui.css');
     echo $this->fetch('css');
     //追加部分
     echo $this->Html->script('//code.jquery.com/jquery-1.11.3.min.js');
     echo $this->Html->script('//code.jquery.com/ui/1.11.4/jquery-ui.min.js');
     echo $this->Html->script('//ajax.googleapis.com/ajax/libs/jqueryui/1/i18n/jquery.ui.datepicker-ja.min.js');
     echo $this->fetch('script');
    

デプロイメントの設定

※IntelliJ IDEAを基準に書いているため、他の開発環境では異なる可能性があります。

項目 内容
デプロイメント元 {プロジェクトディレクトリ}/{開発用ディレクトリ}
デプロイメント先 {ドキュメントルート}/{サイト名}
デプロイメントから除外するファイル .svn
.cvs
.idea
.DS_Store*
.git*
.hg
build*
*.md
.editorconfig
*.yml
LICENSE.txt
composer.json
.jshintrc
._*
.Spotlight-V100
.Trashes
Icon?
*.db
*.diff
*.err
*.orig
*.rej
*.swo
*.swp
*.vi
*~
.cache
.project
.settings
nbproject
tags
dist
*.mo
*.default
*.lock
.classpath
.eml
bin
README*
license
*.eml
*.zip

実行環境の設定

前提

  • 管理者権限が使えない。
  • SQLite3がインストールされている。

手順

  1. 開発環境から実行環境へデプロイメントを行います。
  2. cd {ドキュメントルート}/{サイト名}コマンドを実行し、カレントディレクトリを移動します。
  3. chmod 757 -R app/tmpコマンドを実行し、tmpディレクトリのパーミッションを設定します。
  4. chmod 777 -R app/webroot/uploadコマンドを実行し、uploadディレクトリのパーミッションを設定します。
  5. chmod 707 dbコマンドを実行し、dbディレクトリのパーミッションを設定します。
  6. chmod 606 db/{DBファイル名}コマンドを実行し、SQLite用DBファイルのパーミッションを設定します。
  7. CakePHPを導入したサイトにアクセスし、出てきたページに(DBに何も入っていないという警告を除く)エラーや警告が表示されてなければ、導入に成功したということになります。

参考文献

1
1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?