
Cakephp2からCakephp3や4へのアップデートって、根幹から変わっている部分が多く(配列ベースからオブジェクトベースへ)元コードで稼働するケースはほぼないと言っても良いほど、破壊的な変更が加えられています。
そこで本記事では View 系を中心に、ちっぷすとしてシンプルに箇条書きでメモを残しておきます。
View系置き換え(Cake2コード → Cake3/4コード形式)
・JSやCSSなどの読み込み
// Cakephp2 'inline' => false // Cakephp4 'block' => true
・パンくず
// Cakephp2 $this->Html->addCrumb() // Cakephp4 $this->Breadcrumbs->add()
・コントローラー名取得
// Cakephp2 $this->name // Cakephp4 $this->getName()
・アクション名取得
// Cakephp2
$this->action
// Cakephp4
$this->getRequest()->getParam('action')
・HTMLヘルパーURL生成
// Cakephp2 $this->Html->url() // Cakephp4 $this->Url->build()
・Layoutテンプレート
// Cakephp2
$script_for_layout
// Cakephp4
$this->fetch('script')
// Cakephp2
$content_for_layout
// Cakephp4
$this->fetch('content')
// Viewはセットで記述
$this->fetch('meta')
$this->fetch('css')
$this->fetch('script')
・ルーティングパラメーター取得
// Cakephp2
$this->here
// Cakephp4
$this->getRequest()->getAttribute('here')
Controller系(Cake2コード → Cake3/4コード形式)
・レイアウト設定
// Cakephp2
$this->layout
// Cakephp4
$this->ViewBuilder()->setLayout('hoge')
・レスポンス系
// Cakephp2 $this->response->statusCode(ステータス番号) // Cakephp4 $this->response->withStatus(ステータス番号)
・メールCake4系: Mailer
use Cake\Mailer\Mailer;
// メソッド内に記述
$mailer = new Mailer('default');
$mailer->setFrom(['me@example.com' => 'My Site'])
->setTo('you@example.com')
->setSubject('About')
->deliver('My message');
これだけでもレガシーなシステムから移行する際に役立つのではと思います。