Cakephp WEB開発

Cake3とCake4でCSRFトークンの取得の仕方が微妙に違う

※本サイトはPR表記を含みます。

 

Cakephp3Cakephp4ではCSRFトークンの取得の仕方がビミョーに違います。

「トークンの値をちょっと取得したいなー」って時、「えっ?なんで取れへんの?」というのを少しでも避けたい。

 

csrfTokenの取得の仕方(デフォルト。命名変更する場合、FormHelperの設定も必要)

Cake3: CsrfProtectionMiddleware

 shortcode
<?= $this->request->getParam('_csrfToken') ?>

https://book.cakephp.org/3/ja/controllers/middleware.html#csrf

 

Cake4: CsrfProtectionMiddleware

 shortcode
<?= $this->request->getAttribute('csrfToken') ?>

https://book.cakephp.org/4/ja/security/csrf.html#id2

 

 

 

-Cakephp, WEB開発
-