Contents
composerのインストール
パス指定なしでも実行できるよう/usr/binに置く。(PATHが通っている所であれば/usr/bin以外でもOK。
但し/usr/local/bin等に置く場合は、ユーザ環境以外にsudoでも利用可能な環境にする必要がある。これはvisudoの中のsecure_pathとして定義するので、ごちゃごちゃファイルが入っているディレクトリを指定するのはセキュリティ上避けた方が良いだろう。)
1 2 | curl -sS https://getcomposer.org/installer | php sudo mv composer.phar /usr/bin/composer |
git, etc…のインストール
1 | sudo yum -y install git unzip |
データベースにPostgreSQLを使用する場合
1 2 | rpm -i https://download.postgresql.org/pub/repos/yum/reporpms/EL-8-x86_64/pgdg-redhat-repo-latest.noarch.rpm yum install postgresql12-server postgresql12-libs php-pgsql |
CakePHP3のプロジェクト作成
1 | sudo -i /usr/local/bin/composer self-update && composer create-project --prefer-dist cakephp/app <プロジェクト名> |
で、<プロジェクト名>のディレクトリが作成され、その中に各種ファイルが生成される。
ちなみに
1 | composer self-update && |
はcomposer自身をアップデートしてからCakePHP3のプロジェクトを作成する。
また、&&はcomposer自身のアップデートでエラーが起きたらCakePHP3のプロジェクトは作成されない。
||で繋ぐとエラーが起きても次のコマンドを実行し、;(セミコロン)の場合は、エラーに関係なく次のコマンドが実行される。
UserDir設定
環境によっては、ユーザディレクトリ対応がうまく動かない。
※ユーザディレクトリの前にデフォルトのドキュメントルートが付加された形でrewriteされる場合がある。
この場合はRewriteBaseを.htaccessに設定するがgitに登録してしまうと他の人に影響が出てしまうので、最初にデフォルトの.gitignoreを登録した後、変更を無視する設定を行う。
1 2 3 4 5 6 7 | cd <プロジェクトフォルダ> git init git remote add origin ssh://git@<gitホスト>:<sshポート番号>/<リポジトリURL>/<プロジェクト>.git git add . git commit -m "Initial commit" git push -u origin master vi .htacess webroot/.htaccess |
1 2 3 4 5 6 7 8 9 10 11 12 | # Uncomment the following to prevent the httpoxy vulnerability # See: https://httpoxy.org/ #<IfModule mod_headers.c> # RequestHeader unset Proxy #</IfModule> <IfModule mod_rewrite.c> RewriteEngine on RewriteBase /~<ユーザID>/<プロジェクト名> RewriteRule ^$ webroot/ [L] RewriteRule (.*) webroot/$1 [L] </IfModule> |
1 2 3 4 5 6 | <IfModule mod_rewrite.c> RewriteEngine On RewriteBase /~<ユーザID>/<プロジェクト名>/webroot RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^ index.php [L] </IfModule> |
1 2 3 4 | git update-index --skip-worktree .htaccess git update-index --skip-worktree webroot/.htaccess vi .gitignore <.htaccess webroot/.htaccessを追加> |
1 2 | git commit -a -m "modify .gitignore" git push |
PostgreSQLの接続設定
CakePHP3でPostgreSQLを利用するときの設定
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | 'Datasources' => [ 'default' => [ 'className' => 'Cake\Database\Connection', 'driver' => 'Cake\Database\Driver\Postgres', 'persistent' => false, 'host' => 'localhost', /** * CakePHP will use the default DB port based on the driver selected * MySQL on MAMP uses port 8889, MAMP users will want to uncomment * the following line and set the port accordingly */ //'port' => 'non_standard_port_number', 'username' => '<ユーザー名>', 'password' => '<パスワード>', 'database' => '<DB名>', 'encoding' => 'utf8', 'timezone' => '+09:00', 'flags' => [], 'cacheMetadata' => true, 'log' => false, |
すぐ下にあるtest用のコネクション定義も<DB名>以外を同様に設定。
最近のバージョンでは「’driver’ => Mysql::class,」となっていたりする。この場合は
1 | 'driver' => Postgres::class, |
とし、useで
1 | use Cake\Database\Driver\Postgres; |
とする。なおDebugKitでMySQLを使用するらしく、「use Cake\Database\Driver\Mysql;」は残しておく事。
タイムゾーン
デフォルトはUTCになっているので’Asia/Tokyo’に変更する。
1 2 3 4 5 | /** * Set server timezone to UTC. You can change it to another timezone of your * choice but using UTC makes time calculations / conversions easier. */ date_default_timezone_set('Asia/Tokyo'); |
composerの高速化
日本のミラーに設定しレスポンスを良くする
1 2 3 4 | composer config -g repositories.packagist composer https://packagist.jp # 元に戻す場合はunsetする composer config -g --unset repositories.packagist |
コーディング規約
公式のコーディング規約
https://book.cakephp.org/3.0/ja/contributing/cakephp-coding-conventions.html(日本語)
composerで追加
1 2 | sudo yum install -y php-xml composer require cakephp/cakephp-codesniffer |
CakePHPにセット
1 | sudo vendor/bin/phpcs --config-set installed_paths vendor/cakephp/cakephp-codesniffer |
コードチェック
1 2 | vendor/bin/phpcs --standard=CakePHP src/ vendor/bin/phpcs --standard=CakePHP src/ --report=diff |