ブログチュートリアルで作った「blog」のアーカイブ記事の検索フォーム作るメモ。

入力されたキーワードのタイトルがあれば表示される。
blog/src/Template/Articles/find.ctp ファイルを作成する。
<div>
<h3>Find Articles</h3>
<?= $msg ?>
<?= $this->Form->create() ?>
<fieldset>
<?= $this->Form->input('find'); ?>
<?= $this->Form->button('Submit') ?>
<?= $this->Form->end() ?>
</fieldset>
<table>
<thead>
<tr>
<th>id</th>
<th>title</th>
<th>created</th>
<th>modified</th>
</tr>
</thead>
<tbody>
<?php foreach ($articles as $article): ?>
<tr>
<td><?= h($article->id) ?></td>
<td><?= h($article->title) ?></td>
<td><?= h($article->created) ?></td>
<td><?= h($article->modified) ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
blog/src/Controller/PersonsController.php ファイルに検索メソッドを追加する。
/**
* 検索メソッド
*/
public function find() {
$articles = [];
if ($this->request->is('post')) {
$find = $this->request->data['find'];
$articles = $this->Articles->find()
->where(["title like " => '%' . $find . '%']);
} $this->set('msg', null);
$this->set('articles', $articles);
}