CakePHP3のbakeコマンドで雛形ファイルを生成する。
目次
・ bakeコマンド実行(usersの全雛形ファイル生成)
・ bakeコマンドで作成されたページ確認(/users/add)
・ 【参考】生成されたモデル[Table]雛形ファイル(Model/Table/UsersTable.php)
・ 【参考】生成されたモデル[Entity]雛形ファイル(Model/Entity/User.php)
・ 【参考】生成されたビュー雛形ファイル(Template/Users/index.ctp)
・ 【参考】生成されたコントローラー雛形ファイル(Controller/UsersController.php)
● 使用例のテーブル定義(usersテーブル)
1 2 3 4 5 6 7 8 9 10 | // ユーザーテーブル // status 0:仮登録 1:本登録 2:退会 CREATE TABLE `users` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT PRIMARY KEY, `email` varchar(255) NOT NULL, `password` varchar(255) NOT NULL, `status` char(1) DEFAULT 0, `created` DATETIME DEFAULT NULL, `modified` DATETIME DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; |
◯ bakeコマンド実行(usersの全雛形ファイル生成)
※上記のusersテーブルをDBに作成後実行する
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | // 雛形ファイル生成 # bin/cake bake all users Bake All --------------------------------------------------------------- One moment while associations are detected. Baking table class for Users... Creating file /var/www/html/cakephp3.com/src/Model/Table/UsersTable.php Wrote `/var/www/html/cakephp3.com/src/Model/Table/UsersTable.php` Deleted `/var/www/html/cakephp3.com/src/Model/Table/empty` Baking entity class for User... Creating file /var/www/html/cakephp3.com/src/Model/Entity/User.php Wrote `/var/www/html/cakephp3.com/src/Model/Entity/User.php` Deleted `/var/www/html/cakephp3.com/src/Model/Entity/empty` Baking test fixture for Users... Creating file /var/www/html/cakephp3.com/tests/Fixture/UsersFixture.php Wrote `/var/www/html/cakephp3.com/tests/Fixture/UsersFixture.php` Deleted `/var/www/html/cakephp3.com/tests/Fixture/empty` Bake is detecting possible fixtures... Baking test case for App\Model\Table\UsersTable ... Creating file /var/www/html/cakephp3.com/tests/TestCase/Model/Table/UsersTableTest.php Wrote `/var/www/html/cakephp3.com/tests/TestCase/Model/Table/UsersTableTest.php` Baking controller class for Users... Creating file /var/www/html/cakephp3.com/src/Controller/UsersController.php Wrote `/var/www/html/cakephp3.com/src/Controller/UsersController.php` Bake is detecting possible fixtures... Baking test case for App\Controller\UsersController ... Creating file /var/www/html/cakephp3.com/tests/TestCase/Controller/UsersControllerTest.php Wrote `/var/www/html/cakephp3.com/tests/TestCase/Controller/UsersControllerTest.php` Baking `index` view template file... Creating file /var/www/html/cakephp3.com/src/Template/Users/index.ctp Wrote `/var/www/html/cakephp3.com/src/Template/Users/index.ctp` Baking `view` view template file... Creating file /var/www/html/cakephp3.com/src/Template/Users/view.ctp Wrote `/var/www/html/cakephp3.com/src/Template/Users/view.ctp` Baking `add` view template file... Creating file /var/www/html/cakephp3.com/src/Template/Users/add.ctp Wrote `/var/www/html/cakephp3.com/src/Template/Users/add.ctp` Baking `edit` view template file... Creating file /var/www/html/cakephp3.com/src/Template/Users/edit.ctp Wrote `/var/www/html/cakephp3.com/src/Template/Users/edit.ctp` Bake All complete. |
◯ bakeコマンドで作成されたページ確認(/users/add)
usersテーブル作成してbakeコマンド実行しただけで、ユーザー情報をCRUD(作成・閲覧・編集・削除)できるユーザー管理ページが作成されています。
ユーザー新規作成ページ https://開発環境のURL/users/add
◯ 【参考】生成されたモデル[Table]雛形ファイル(Model/Table/UsersTable.php)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | # cat src/Model/Table/UsersTable.php <?php namespace App\Model\Table; use Cake\ORM\Query; use Cake\ORM\RulesChecker; use Cake\ORM\Table; use Cake\Validation\Validator; /** * Users Model * * @method \App\Model\Entity\User get($primaryKey, $options = []) * @method \App\Model\Entity\User newEntity($data = null, array $options = []) * @method \App\Model\Entity\User[] newEntities(array $data, array $options = []) * @method \App\Model\Entity\User|bool save(\Cake\Datasource\EntityInterface $entity, $options = []) * @method \App\Model\Entity\User patchEntity(\Cake\Datasource\EntityInterface $entity, array $data, array $options = []) * @method \App\Model\Entity\User[] patchEntities($entities, array $data, array $options = []) * @method \App\Model\Entity\User findOrCreate($search, callable $callback = null, $options = []) * * @mixin \Cake\ORM\Behavior\TimestampBehavior */ class UsersTable extends Table { /** * Initialize method * * @param array $config The configuration for the Table. * @return void */ public function initialize(array $config) { parent::initialize($config); $this->setTable('users'); $this->setDisplayField('id'); $this->setPrimaryKey('id'); $this->addBehavior('Timestamp'); } /** * Default validation rules. * * @param \Cake\Validation\Validator $validator Validator instance. * @return \Cake\Validation\Validator */ public function validationDefault(Validator $validator) { $validator ->integer('id') ->allowEmpty('id', 'create'); $validator ->email('email') ->requirePresence('email', 'create') ->notEmpty('email'); $validator ->scalar('password') ->requirePresence('password', 'create') ->notEmpty('password'); $validator ->scalar('status') ->allowEmpty('status'); return $validator; } /** * Returns a rules checker object that will be used for validating * application integrity. * * @param \Cake\ORM\RulesChecker $rules The rules object to be modified. * @return \Cake\ORM\RulesChecker */ public function buildRules(RulesChecker $rules) { $rules->add($rules->isUnique(['email'])); return $rules; } } |
◯ 【参考】生成されたモデル[Entity]雛形ファイル(Model/Entity/User.php)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | # cat src/Model/Entity/User.php <?php namespace App\Model\Entity; use Cake\ORM\Entity; /** * User Entity * * @property int $id * @property string $email * @property string $password * @property string $status * @property \Cake\I18n\FrozenTime $created * @property \Cake\I18n\FrozenTime $modified */ class User extends Entity { /** * Fields that can be mass assigned using newEntity() or patchEntity(). * * Note that when '*' is set to true, this allows all unspecified fields to * be mass assigned. For security purposes, it is advised to set '*' to false * (or remove it), and explicitly make individual fields accessible as needed. * * @var array */ protected $_accessible = [ 'email' => true, 'password' => true, 'status' => true, 'created' => true, 'modified' => true ]; /** * Fields that are excluded from JSON versions of the entity. * * @var array */ protected $_hidden = [ 'password' ]; } |
◯ 【参考】生成されたビュー雛形ファイル(Template/Users/index.ctp)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | # cat src/Template/Users/index.ctp <?php /** * @var \App\View\AppView $this * @var \App\Model\Entity\User[]|\Cake\Collection\CollectionInterface $users */ ?> <nav class="large-3 medium-4 columns" id="actions-sidebar"> <ul class="side-nav"> <li class="heading"><?= __('Actions') ?></li> <li><?= $this->Html->link(__('New User'), ['action' => 'add']) ?></li> </ul> </nav> <div class="users index large-9 medium-8 columns content"> <h3><?= __('Users') ?></h3> <table cellpadding="0" cellspacing="0"> <thead> <tr> <th scope="col"><?= $this->Paginator->sort('id') ?></th> <th scope="col"><?= $this->Paginator->sort('email') ?></th> <th scope="col"><?= $this->Paginator->sort('password') ?></th> <th scope="col"><?= $this->Paginator->sort('status') ?></th> <th scope="col"><?= $this->Paginator->sort('created') ?></th> <th scope="col"><?= $this->Paginator->sort('modified') ?></th> <th scope="col" class="actions"><?= __('Actions') ?></th> </tr> </thead> <tbody> <?php foreach ($users as $user): ?> <tr> <td><?= $this->Number->format($user->id) ?></td> <td><?= h($user->email) ?></td> <td><?= h($user->password) ?></td> <td><?= h($user->status) ?></td> <td><?= h($user->created) ?></td> <td><?= h($user->modified) ?></td> <td class="actions"> <?= $this->Html->link(__('View'), ['action' => 'view', $user->id]) ?> <?= $this->Html->link(__('Edit'), ['action' => 'edit', $user->id]) ?> <?= $this->Form->postLink(__('Delete'), ['action' => 'delete', $user->id], ['confirm' => __('Are you sure you want to delete # {0}?', $user->id)]) ?> </td> </tr> <?php endforeach; ?> </tbody> </table> <div class="paginator"> <ul class="pagination"> <?= $this->Paginator->first('<< ' . __('first')) ?> <?= $this->Paginator->prev('< ' . __('previous')) ?> <?= $this->Paginator->numbers() ?> <?= $this->Paginator->next(__('next') . ' >') ?> <?= $this->Paginator->last(__('last') . ' >>') ?> </ul> <p><?= $this->Paginator->counter(['format' => __('Page {{page}} of {{pages}}, showing {{current}} record(s) out of {{count}} total')]) ?></p> </div> </div> |
◯ 【参考】生成されたコントローラー雛形ファイル(Controller/UsersController.php)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 | # cat src/Controller/UsersController.php <?php namespace App\Controller; use App\Controller\AppController; /** * Users Controller * * @property \App\Model\Table\UsersTable $Users * * @method \App\Model\Entity\User[] paginate($object = null, array $settings = []) */ class UsersController extends AppController { /** * Index method * * @return \Cake\Http\Response|void */ public function index() { $users = $this->paginate($this->Users); $this->set(compact('users')); $this->set('_serialize', ['users']); } /** * View method * * @param string|null $id User id. * @return \Cake\Http\Response|void * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found. */ public function view($id = null) { $user = $this->Users->get($id, [ 'contain' => [] ]); $this->set('user', $user); $this->set('_serialize', ['user']); } /** * Add method * * @return \Cake\Http\Response|null Redirects on successful add, renders view otherwise. */ public function add() { $user = $this->Users->newEntity(); if ($this->request->is('post')) { $user = $this->Users->patchEntity($user, $this->request->getData()); if ($this->Users->save($user)) { $this->Flash->success(__('The user has been saved.')); return $this->redirect(['action' => 'index']); } $this->Flash->error(__('The user could not be saved. Please, try again.')); } $this->set(compact('user')); $this->set('_serialize', ['user']); } /** * Edit method * * @param string|null $id User id. * @return \Cake\Http\Response|null Redirects on successful edit, renders view otherwise. * @throws \Cake\Network\Exception\NotFoundException When record not found. */ public function edit($id = null) { $user = $this->Users->get($id, [ 'contain' => [] ]); if ($this->request->is(['patch', 'post', 'put'])) { $user = $this->Users->patchEntity($user, $this->request->getData()); if ($this->Users->save($user)) { $this->Flash->success(__('The user has been saved.')); return $this->redirect(['action' => 'index']); } $this->Flash->error(__('The user could not be saved. Please, try again.')); } $this->set(compact('user')); $this->set('_serialize', ['user']); } /** * Delete method * * @param string|null $id User id. * @return \Cake\Http\Response|null Redirects to index. * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found. */ public function delete($id = null) { $this->request->allowMethod(['post', 'delete']); $user = $this->Users->get($id); if ($this->Users->delete($user)) { $this->Flash->success(__('The user has been deleted.')); } else { $this->Flash->error(__('The user could not be deleted. Please, try again.')); } return $this->redirect(['action' => 'index']); } } |