/src/Model/Table/PostsTable.php
namespace App\Model\Table; use Cake\ORM\Table; class PostsTable extends Table { public function initialize(array $config) : void { $this->addBehavior('Timestamp'); } }
/templates/Posts/index.php
L 拡張子はctpではなくphpにする
https://book.cakephp.org/4/ja/views.html
<h1>Blog Posts</h1> <ul> <?php foreach($posts as $post) : ?> <li><?= h($post->title); ?></li> <?php endforeach;?> </ul>
/src/Controller/PostsController.php
// /posts/index // /(controller)/(action)/(options) namespace App\Controller; class PostsController extends AppController{ public function index(){ $posts = $this->Posts->find('all'); $this->set('posts', $posts); } }
config/routes.php
$builder->connect('/', ['controller' => 'Posts', 'action' => 'index']);
controller
$posts = $this->Posts->find('all')->order(['title'=>'DESC'])->limit(2)->where(['title like'=> '%3']);
3系と4系だと、大分書き方が変わってるな。特にctpからphpに変わってるのは驚きだな。