Home>

https://book.cakephp.org/4/ja/controllers/request-response.html#cake-request

The page above the cookbook in the official manual says, "The default request is set to $this->request and is available in controllers, cells, views, and helpers."

In Controller$this->requestWorks fine, but it doesn't seem to exist in Helpervar_dump ($this->request)When you donullIt will be.

Could you please tell me how to use $this->request in Helper?


testing environment

  • CakePHP 4.1.6
  • PHP 7.4.12
  • Apahce 2.4.6
  • Answer # 1

    In the Helper class$this->requestIs deprecated in 3.7 and removed in 4.0 and later.

    $this->getView ()->getRequest ();

    Please get as.

    The page above the cookbook in the official manual says, "The default request is set to $this->request and is available in controllers, cells, views, and helpers."

    This is a story in 3.x, and the description in the document is incorrect. (The English version is also wrong ...

  • Answer # 2

    I don't wonder if I want to make something, but I will interpret it as a question that I wondered while reading the cookbook and answer it.

    If you refer to the helper in the cookbook, you can see how to use $this->request in the helper. ([Loading conditional helpers] https://book.cakephp.org/4/ja/views/helpers.html)

    class AppView extends View
    {
        public function initialize (): void
        {
            parent :: initialize ();
            if ($this->request->getParam ('action') ==='index') {
                $this->loadHelper ('ListPage');
            }
        }
    }

    The above seems to get the currently accessed action and branch it.
    Probably, the general usage of $this->request in the helper is to get the request parameter (getParam method) and branch the loading of the helper.