Home > Enterprise >  How to get $this->request->getAttribute('csrfToken') from Helper in Cakephp4?
How to get $this->request->getAttribute('csrfToken') from Helper in Cakephp4?

Time:12-14

I need to get csrf token from a Helper to use it in an ajax request.

But when I try to get it using :

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

$this->request is null from the helper.

Someone to tell me how to do please ?

CodePudding user response:

Helpers do not receive the request instance, but the view instance which they are attached to, so you can obtain the request from there.

$this->getView()->getRequest()->getAttribute('csrfToken')
  • Related