その他
DebugKitを非表示にする方法
config/bootstrap.php を変更することで、DebugKitの表示を切り替えることができます。
以下の設定をfalseにすることで、DebugKitをロードしようにしています。
/* * Only try to load DebugKit in development mode * Debug Kit should not be installed on a production system */ if (Configure::read('debug')) { Plugin::load('DebugKit', ['bootstrap' => false]); }
DBタイムゾーンを設定する
デフォルトでは、Dbのタイムゾーン設定がUTCになっているので、Asia/Tokyoに変更する方法です。
以下の2箇所を変更します。
config/app.phpのDatasourcesのtimezoneを変更します。
'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' => 'xxxxxxxx', 'password' => 'xxxxxxxx', 'database' => 'xxxxxxxx', 'encoding' => 'utf8', 'timezone' => 'Asia/Tokyo',// ここを変更します。 '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'], ],
config/bootstrap.phpのdate_default_timezone_set()を変更します。
/** * 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');
テーブルのカラム名を取得する
テーブルのカラム名を取得する方法です。
$usersTable = TableRegistry::get (Users); $columns = $usersTable->schema()->columns(); var_dump($columns); ■実行結果 array(6) { [0]=> string(9) "user_id" [1]=> string(11) "user_name" [2]=> string(8) "modified" [3]=> string(7) "created" }
ファイル名と行番号を出力してデバックする
CakePHPには、debugという関数が用意されています。
ファイル名と行番号を出力することができます。
この場合は、第三引数をtrueにします。(デフォルトはfalse)
$aisatsu = "HELLO!!"; debug($aisatsu, false, true); ◆出力結果 app/controllers/tests_controller.php (line 145) HELLO!!
HTMLタグを見やすくデバックする
CakePHPには、debugという関数が用意されています。
第二引数をtrueにすると(デフォルトはfalse)‘<'と'>‘をHTML特殊文字に変換して出力してくれます。
これで、ブラウザでタグの内容が見やすくなります。
$aisatsu = '<b>HELLO!!</b>'; debug($aisatsu, true); ◆出力結果 <b>HELLO!!</b>
配列を見やすくデバックをする
CakePHPには、debugという関数が用意されていて、var_dump()よりも配列が見やすくデバックができます。
$aisatsu = [ 'おはよう', 'こんにちは', 'こんばんは' ]; debug($aisatsu); ◆出力結果 Array ( [0] => おはよう [1] => こんにちは [2] => こんばんは )
SQLのクエリをログに出力する
クエリログを有効にすると、SQLのクエリをログに出力することができます。
デフォルトでは、無効に設定されていいます。
config/app.php(13行目あたり)に以下の設定をtrueにするとクエリをログに出力できます。
'debug' => filter_var(env('DEBUG', true), FILTER_VALIDATE_BOOLEAN),
出力場所は、logs/配下に出力されます。
ログとキャッシュのマスク(パーミッション)を設定する
マスクとは、生成するファイルのパーミッション設定のことです。
設定は、config/app.phpで設定できます。
まずはキャッシュです。
/** * Configure the cache adapters. */ 'Cache' => [ 'default' => [ 'className' => 'File', 'path' => CACHE, 'url' => env('CACHE_DEFAULT_URL', null), ], /** * Configure the cache used for general framework caching. * Translation cache files are stored with this configuration. * Duration will be set to '+1 year' in bootstrap.php when debug = false */ '_cake_core_' => [ 'className' => 'File', 'prefix' => 'myapp_cake_core_', 'path' => CACHE . 'persistent/', 'serialize' => true, 'duration' => '+2 minutes', 'url' => env('CACHE_CAKECORE_URL', null), 'mask' => 0666 // ここを追加 ], /** * Configure the cache for model and datasource caches. This cache * configuration is used to store schema descriptions, and table listings * in connections. * Duration will be set to '+1 year' in bootstrap.php when debug = false */ '_cake_model_' => [ 'className' => 'File', 'prefix' => 'myapp_cake_model_', 'path' => CACHE . 'models/', 'serialize' => true, 'duration' => '+2 minutes', 'url' => env('CACHE_CAKEMODEL_URL', null), 'mask' => 0666 // ここを追加 ], ],
次にログです。
/** * Configures logging options */ 'Log' => [ 'debug' => [ 'className' => 'Cake\Log\Engine\FileLog', 'path' => LOGS, 'file' => 'debug', 'levels' => ['notice', 'info', 'debug'], 'mask' => 0666 // ここを追加 ], 'error' => [ 'className' => 'Cake\Log\Engine\FileLog', 'path' => LOGS, 'file' => 'error', 'levels' => ['warning', 'error', 'critical', 'alert', 'emergency'], 'mask' => 0666 // ここを追加 ], ],
------------------------------
作成日:2016年07月14日
更新日:2017年06月25日
------------------------------