はじめに
こんにちは、エンジニアのkeitaMaxです。
前回の記事の続きです。
今回はコントローラとモデルを作成します。
コントローラを作成
以下コマンドでコントローラを作成します。
bin/cake bake controller articles
実行すると以下のようになります。
root@04a17a8a001b:/app# bin/cake bake controller articles
Baking controller class for Articles...
Creating file /app/src/Controller/ArticlesController.php
Wrote `/app/src/Controller/ArticlesController.php`
Bake is detecting possible fixtures...
Baking test case for App\Controller\ArticlesController ...
Creating file /app/tests/TestCase/Controller/ArticlesControllerTest.php
Wrote `/app/tests/TestCase/Controller/ArticlesControllerTest.php`
Done
root@04a17a8a001b:/app#
src/Controller/ArticlesController.php
ファイルととtests/TestCase/Controller/ArticlesControllerTest.php
ファイルが作成されます。
コントローラだけでなく、コントローラのテストをするファイルも一緒に作成されます。
<?php
declare(strict_types=1);
namespace App\Controller;
/**
* Articles Controller
*
*/
class ArticlesController extends AppController
{
/**
* Index method
*
* @return \Cake\Http\Response|null|void Renders view
*/
public function index()
{
$query = $this->Articles->find();
$articles = $this->paginate($query);
$this->set(compact('articles'));
}
/**
* View method
*
* @param string|null $id Article id.
* @return \Cake\Http\Response|null|void Renders view
* @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
*/
public function view($id = null)
{
$article = $this->Articles->get($id, contain: []);
$this->set(compact('article'));
}
/**
* Add method
*
* @return \Cake\Http\Response|null|void Redirects on successful add, renders view otherwise.
*/
public function add()
{
$article = $this->Articles->newEmptyEntity();
if ($this->request->is('post')) {
$article = $this->Articles->patchEntity($article, $this->request->getData());
if ($this->Articles->save($article)) {
$this->Flash->success(__('The article has been saved.'));
return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('The article could not be saved. Please, try again.'));
}
$this->set(compact('article'));
}
/**
* Edit method
*
* @param string|null $id Article id.
* @return \Cake\Http\Response|null|void Redirects on successful edit, renders view otherwise.
* @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
*/
public function edit($id = null)
{
$article = $this->Articles->get($id, contain: []);
if ($this->request->is(['patch', 'post', 'put'])) {
$article = $this->Articles->patchEntity($article, $this->request->getData());
if ($this->Articles->save($article)) {
$this->Flash->success(__('The article has been saved.'));
return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('The article could not be saved. Please, try again.'));
}
$this->set(compact('article'));
}
/**
* Delete method
*
* @param string|null $id Article 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']);
$article = $this->Articles->get($id);
if ($this->Articles->delete($article)) {
$this->Flash->success(__('The article has been deleted.'));
} else {
$this->Flash->error(__('The article could not be deleted. Please, try again.'));
}
return $this->redirect(['action' => 'index']);
}
}
<?php
declare(strict_types=1);
namespace App\Test\TestCase\Controller;
use App\Controller\ArticlesController;
use Cake\TestSuite\IntegrationTestTrait;
use Cake\TestSuite\TestCase;
/**
* App\Controller\ArticlesController Test Case
*
* @uses \App\Controller\ArticlesController
*/
class ArticlesControllerTest extends TestCase
{
use IntegrationTestTrait;
/**
* Fixtures
*
* @var list<string>
*/
protected array $fixtures = [
'app.Articles',
];
/**
* Test index method
*
* @return void
* @uses \App\Controller\ArticlesController::index()
*/
public function testIndex(): void
{
$this->markTestIncomplete('Not implemented yet.');
}
/**
* Test view method
*
* @return void
* @uses \App\Controller\ArticlesController::view()
*/
public function testView(): void
{
$this->markTestIncomplete('Not implemented yet.');
}
/**
* Test add method
*
* @return void
* @uses \App\Controller\ArticlesController::add()
*/
public function testAdd(): void
{
$this->markTestIncomplete('Not implemented yet.');
}
/**
* Test edit method
*
* @return void
* @uses \App\Controller\ArticlesController::edit()
*/
public function testEdit(): void
{
$this->markTestIncomplete('Not implemented yet.');
}
/**
* Test delete method
*
* @return void
* @uses \App\Controller\ArticlesController::delete()
*/
public function testDelete(): void
{
$this->markTestIncomplete('Not implemented yet.');
}
}
モデルの作成
以下のコマンドでModelを作成します。
bin/cake bake model articles
articlesテーブルをもとにModelを作ってくれるので、データベースにarticlesテーブルがないとエラーになるので注意が必要です。
root@04a17a8a001b:/app# bin/cake bake model articles
One moment while associations are detected.
Baking table class for Articles...
Creating file /app/src/Model/Table/ArticlesTable.php
Wrote `/app/src/Model/Table/ArticlesTable.php`
Deleted `/app/src/Model/Table/.gitkeep`
Baking entity class for Article...
Creating file /app/src/Model/Entity/Article.php
Wrote `/app/src/Model/Entity/Article.php`
Deleted `/app/src/Model/Entity/.gitkeep`
Baking test fixture for Articles...
Creating file /app/tests/Fixture/ArticlesFixture.php
Wrote `/app/tests/Fixture/ArticlesFixture.php`
Deleted `/app/tests/Fixture/.gitkeep`
Bake is detecting possible fixtures...
Baking test case for App\Model\Table\ArticlesTable ...
Creating file /app/tests/TestCase/Model/Table/ArticlesTableTest.php
Wrote `/app/tests/TestCase/Model/Table/ArticlesTableTest.php`
Done
root@04a17a8a001b:/app#
以下4ファイルが作成されます。
src/Model/Table/ArticlesTable.php
src/Model/Entity/Article.php
tests/Fixture/ArticlesFixture.php
tests/TestCase/Model/Table/ArticlesTableTest.php
Modelと一緒にそれをテストするためのファイルも一緒に作成されています。
<?php
declare(strict_types=1);
namespace App\Model\Table;
use Cake\ORM\Query\SelectQuery;
use Cake\ORM\RulesChecker;
use Cake\ORM\Table;
use Cake\Validation\Validator;
/**
* Articles Model
*
* @method \App\Model\Entity\Article newEmptyEntity()
* @method \App\Model\Entity\Article newEntity(array $data, array $options = [])
* @method array<\App\Model\Entity\Article> newEntities(array $data, array $options = [])
* @method \App\Model\Entity\Article get(mixed $primaryKey, array|string $finder = 'all', \Psr\SimpleCache\CacheInterface|string|null $cache = null, \Closure|string|null $cacheKey = null, mixed ...$args)
* @method \App\Model\Entity\Article findOrCreate($search, ?callable $callback = null, array $options = [])
* @method \App\Model\Entity\Article patchEntity(\Cake\Datasource\EntityInterface $entity, array $data, array $options = [])
* @method array<\App\Model\Entity\Article> patchEntities(iterable $entities, array $data, array $options = [])
* @method \App\Model\Entity\Article|false save(\Cake\Datasource\EntityInterface $entity, array $options = [])
* @method \App\Model\Entity\Article saveOrFail(\Cake\Datasource\EntityInterface $entity, array $options = [])
* @method iterable<\App\Model\Entity\Article>|\Cake\Datasource\ResultSetInterface<\App\Model\Entity\Article>|false saveMany(iterable $entities, array $options = [])
* @method iterable<\App\Model\Entity\Article>|\Cake\Datasource\ResultSetInterface<\App\Model\Entity\Article> saveManyOrFail(iterable $entities, array $options = [])
* @method iterable<\App\Model\Entity\Article>|\Cake\Datasource\ResultSetInterface<\App\Model\Entity\Article>|false deleteMany(iterable $entities, array $options = [])
* @method iterable<\App\Model\Entity\Article>|\Cake\Datasource\ResultSetInterface<\App\Model\Entity\Article> deleteManyOrFail(iterable $entities, array $options = [])
*
* @mixin \Cake\ORM\Behavior\TimestampBehavior
*/
class ArticlesTable extends Table
{
/**
* Initialize method
*
* @param array<string, mixed> $config The configuration for the Table.
* @return void
*/
public function initialize(array $config): void
{
parent::initialize($config);
$this->setTable('articles');
$this->setDisplayField('title');
$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
{
$validator
->scalar('title')
->requirePresence('title', 'create')
->notEmptyString('title');
$validator
->scalar('body')
->requirePresence('body', 'create')
->notEmptyString('body');
return $validator;
}
}
<?php
declare(strict_types=1);
namespace App\Model\Entity;
use Cake\ORM\Entity;
/**
* Article Entity
*
* @property int $id
* @property string $title
* @property string $body
* @property \Cake\I18n\DateTime|null $created
* @property \Cake\I18n\DateTime|null $modified
*/
class Article 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<string, bool>
*/
protected array $_accessible = [
'title' => true,
'body' => true,
'created' => true,
'modified' => true,
];
}
おわりに
これでコントローラとモデルを作成しました。
次回はテストを書いていきたいと思います。
この記事での質問や、間違っている、もっといい方法があるといったご意見などありましたらご指摘していただけると幸いです。
最後まで読んでいただきありがとうございました!