2021/04/30
Cakephp
## プロジェクト作成
```
composer self-update && composer create-project --prefer-dist cakephp/app:4.* my_app_name
```
※ パーミッション変更
```
HTTPDUSER=`ps aux | grep -E '[a]pache|[h]ttpd|[_]www|[w]ww-data|[n]ginx' | grep -v root | head -1 | cut -d -f1`
setfacl -R -m u:${HTTPDUSER}:rwx tmp
setfacl -R -d -m u:${HTTPDUSER}:rwx tmp
setfacl -R -m u:${HTTPDUSER}:rwx logs
setfacl -R -d -m u:${HTTPDUSER}:rwx logs
```
## マイグレーション
### 参考
- [CakePHP4でマイグレーションして、テーブルを作成する](https://specially198.com/migrate-with-cakephp4-and-create-a-table/)
- [公式](https://book.cakephp.org/migrations/3/ja/index.html)
### プラグイン有効化
```
bin/cake plugin load Migrations
```
### マイグレーションファイル作成
```
bin/cake bake migration CreateUsers email:string nickname:string[50] password:string[100] created modified deleted:datetime?
```
```php
declare(strict_types=1);
use MigrationsAbstractMigration;
class CreateUsers extends AbstractMigration
{
public $autoId = false; // uuid使用するため、id自動生成無効
public function change()
{
$table = $this->table('users');
$table->addColumn('id', 'uuid');
$table->addColumn('email', 'string', [
'default' => null,
'limit' => 255,
'null' => false,
]);
$table->addColumn('nickname', 'string', [
'default' => null,
'limit' => 50,
'null' => false,
]);
$table->addColumn('password', 'string', [
'default' => null,
'limit' => 100,
'null' => false,
]);
$table->addColumn('created', 'datetime', [
'default' => 'CURRENT_TIMESTAMP',
'null' => false,
]);
$table->addColumn('modified', 'datetime', [
'default' => 'CURRENT_TIMESTAMP',
'null' => false,
]);
$table->addColumn('deleted', 'datetime', [
'default' => null,
'null' => true,
]);
$table->addPrimaryKey(['id']);
$table->create();
}
}
```
### マイグレーション実行
```
bin/cake migrations migrate
```
### ロールバック
```
bin/cake migrations rollback
```
## 認証機能実装
```
composer require cakephp/authentication:^2.0
```
## コマンド
### 内部サーバー
```
bin/cake server
```
```
bin/cake server -H 192.168.13.37 -p 5673
```