
私の愛しいアップルパイへ
先日、CakePHP 3.xのインストール手順についてお話しました。
今日はインストール後のセットアップ手順についてお話しましょう。
CakePHP3.xのインストール後のセットアップ手順
セットアップといっても、CakePHP 3.xからはかなり手順が簡略化されました。Composer経由のインストールになったことで、今まで手動でセットアップしていたことが自動化されたからです。
データベース接続設定
セットアップといっても基本的にはapp.phpの中でデータベースへの接続情報を記入するだけで、CakePHPが動作するようになるはずです。
▼/configフォルダに移動して、ファイル一覧を見てみてください。app.phpが自動作成されていることを確認します。
app.default.php app.php bootstrap.php bootstrap_cli.php paths.php routes.php schema
▼app.phpを開いてデータベース接続をセットアップします。以下のハイライトされている箇所を修正してみてください。
'Datasources' => [
'default' => [
'className' => 'Cake\Database\Connection',
'driver' => 'Cake\Database\Driver\Mysql',
'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' => 'nonstandard_port_number',
'username' => 'my_app', // データベースへ接続するユーザー名
'password' => 'secret', // データベースへ接続するパスワード
'database' => 'my_app', // 使用するデータベース名
'encoding' => 'utf8',
'timezone' => 'UTC',
'cacheMetadata' => true,
/**
* Set identifier quoting to true if you are using reserved words or
* special characters in your table or column names. Enabling this
* setting will result in queries built using the Query Builder having
* identifiers quoted when creating SQL. It should be noted that this
* decreases performance because each query needs to be traversed and
* manipulated before being executed.
*/
'quoteIdentifiers' => false,
/**
* During development, if using MySQL < 5.6, uncommenting the
* following line could boost the speed at which schema metadata is
* fetched from the database. It can also be set directly with the
* mysql configuration directive 'innodb_stats_on_metadata = 0'
* which is the recommended value in production environments
*/
//'init' => ['SET GLOBAL innodb_stats_on_metadata = 0'],
'unix_socket' => '/Applications/MAMP/tmp/mysql/mysql.sock' // MAMPやXAMMPを使っているときなどで、Socketが見つからないというエラーが発生する場合はこの行を追加する。パスは自分のmysql.sockへのパスを入力すること
],
/**
* The test connection is used during the test suite.
*/
'test' => [
'className' => 'Cake\Database\Connection',
'driver' => 'Cake\Database\Driver\Mysql',
'persistent' => false,
'host' => 'localhost',
//'port' => 'nonstandard_port_number',
'username' => 'my_app', // データベースへ接続するユーザー名(Unitテスト用)
'password' => 'secret', // データベースへ接続するパスワード(Unitテスト用)
'database' => 'test_myapp', // 使用するデータベース名(Unitテスト用、defaultとは別のテーブルを作ること)
'encoding' => 'utf8',
'timezone' => 'UTC',
'cacheMetadata' => true,
'quoteIdentifiers' => false,
//'init' => ['SET GLOBAL innodb_stats_on_metadata = 0'],
'unix_socket' => '/Applications/MAMP/tmp/mysql/mysql.sock' // MAMPやXAMMPを使っているときなどで、Socketが見つからないというエラーが発生する場合はこの行を追加する。パスは自分のmysql.sockへのパスを入力すること
],
],
bake実行時など、以下のようなエラーが発生する場合はsocketが見つかってないときなので、38行目、58行目のようにsokectの場所を指定します。 Warning: mysql_connect(): Can’t connect to local MySQL server through socket
上記例ではtestというデータソースも指定しています。これはPHPUnitを使う場合に使用するデータベース接続となります。PHPUnitを使わないという場合にはtest配列の中は変更不要です。
app.phpが存在しない場合や、他にもディレクトリのパーミッションについてエラーがでる場合、autoload.phpが無くてエラーが出る場合はインストールに失敗していますので、以下の記事を参考に再インストールをオススメします。
タイムゾーンの修正
あと、デフォルトだとタイムゾーンがUTCになっているので、日本時刻に変えたい場合にはbootstrap.phpを修正します。
/**
* 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');
これでCakePHPのセットアップが完了しました。簡単!
貴下の従順なる下僕 松崎より
jMatsuzaki TaskChute Cloud開発者ブログ



