# getRequest in component cakeph 3.8

**URL:** <https://discourse.cakephp.org/t/getrequest-in-component-cakeph-3-8/7044>\
**Category:** Uncategorized\
**Created:** [December 31, 2019, 3:37pm UTC](https://discourse.cakephp.org/t/getrequest-in-component-cakeph-3-8/7044 "2019-12-31T15:37:16Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![danbuntu](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/danbuntu/32/609_2.png) [@danbuntu](https://discourse.cakephp.org/u/danbuntu)\
**Post date:** [December 31, 2019, 3:37pm UTC](https://discourse.cakephp.org/t/getrequest-in-component-cakeph-3-8/7044/1 "2019-12-31T15:37:16Z")

</div>

Hello

I’ve upgraded from 3.4 to 3.8 - yeah I know it’s a little late 🙂

I have a component which is handling setting session variables amongst other things. Previously it have lines like:

$this-\>request-\>session()-\>write(‘Config.settingId’, $settings\_array-\>id);

After the upgrade to 3.8 this is failing - due to I believe $this-\>reesut becoming $this-\>getRequest

So i’ve changed it to:

$session = $this-\>getRequest()-\>getSession();  
$session-\>write(‘Config.settingId’, $settings\_array-\>id);

As in the documentation however it then fail with;

# Call to undefined method App\Controller\Component\MyComponent::getRequest()

Any Ideas please and how I resolve this?

---

<div class="post-metadata">

**Author:** ![dreamingmind](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/dreamingmind/32/1857_2.png) [@dreamingmind](https://discourse.cakephp.org/u/dreamingmind)\
**Post date:** [December 31, 2019, 4:34pm UTC](https://discourse.cakephp.org/t/getrequest-in-component-cakeph-3-8/7044/2 "2019-12-31T16:34:13Z")

</div>

The request is still on the controller so

```php
//$this is your Component
$this->getController()->getRequest()->getSession();

```

---

<div class="post-metadata">

**Author:** ![debendra986](https://avatars.discourse-cdn.com/v4/letter/d/c5a1d2/32.png) [@debendra986](https://discourse.cakephp.org/u/debendra986)\
**Post date:** [March 10, 2021, 10:39am UTC](https://discourse.cakephp.org/t/getrequest-in-component-cakeph-3-8/7044/3 "2021-03-10T10:39:56Z")

</div>

Hello  
I’m facing the same issue in cake 3.8 componet using this overcome from that issue, but now get

> Call to a member function getController() on null  
> how to fix this?

Already add

use Cake\Controller\Controller;

still getting error

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [March 10, 2021, 2:05pm UTC](https://discourse.cakephp.org/t/getrequest-in-component-cakeph-3-8/7044/4 "2021-03-10T14:05:33Z")

</div>

So, `$this` is null in your component? Is it a static function in the component? Otherwise, that should never be the case.

---

<div class="post-metadata">

**Author:** ![dreamingmind](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/dreamingmind/32/1857_2.png) [@dreamingmind](https://discourse.cakephp.org/u/dreamingmind)\
**Post date:** [March 11, 2021, 8:21pm UTC](https://discourse.cakephp.org/t/getrequest-in-component-cakeph-3-8/7044/5 "2021-03-11T20:21:14Z")

</div>

So, you are making a call like this:

```php
$something->getController();

```

The message is telling you that `$something` is NULL.

Look at your code again. It sounds like a simple typo or use of the wrong variable.

If this code is in a component, `$this` holds the component and `$this->getController()` would be the expected call.

---

<div class="post-metadata">

**Author:** ![debendra986](https://avatars.discourse-cdn.com/v4/letter/d/c5a1d2/32.png) [@debendra986](https://discourse.cakephp.org/u/debendra986)\
**Post date:** [March 12, 2021, 9:12am UTC](https://discourse.cakephp.org/t/getrequest-in-component-cakeph-3-8/7044/6 "2021-03-12T09:12:56Z")

</div>

```
namespace App\Controller\Component;

use Cake\Controller\Component;
use Cake\Mailer\Email;
use Cake\Filesystem\Folder;
use Cake\Filesystem\File;
use Cake\Controller\Controller;

class CustomComponent extends Component {

    function __construct($prompt = null) {
        
    }

    function uploadImageBanner($tmp_name, $name, $path, $imgWidth) {
            $user_id = $this->getController()->getRequest()->getSession()->read('Auth.User.id'); **// get error here**
          //code here
        //
    }

}

```

This is my component code  
Can you check what is the issue.

---

<div class="post-metadata">

**Author:** ![thomasg](https://avatars.discourse-cdn.com/v4/letter/t/9d8465/32.png) [@thomasg](https://discourse.cakephp.org/u/thomasg)\
**Post date:** [March 12, 2021, 10:22am UTC](https://discourse.cakephp.org/t/getrequest-in-component-cakeph-3-8/7044/7 "2021-03-12T10:22:22Z")

</div>

$this-\>request-\>getSession(); that should work

maybe the problem is that Auth.User.id not set in the session?

---

<div class="post-metadata">

**Author:** ![debendra986](https://avatars.discourse-cdn.com/v4/letter/d/c5a1d2/32.png) [@debendra986](https://discourse.cakephp.org/u/debendra986)\
**Post date:** [March 12, 2021, 2:16pm UTC](https://discourse.cakephp.org/t/getrequest-in-component-cakeph-3-8/7044/8 "2021-03-12T14:16:14Z")

</div>

Auth.User.id already set in controller when user logged in.

$this-\>request-\>getSession(); is for cake 3.5 or lower  
I’m using cake 3.8.

in cake3.8  
$this-\>request-\>getSession(); //shows deprecated notice

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [March 12, 2021, 2:45pm UTC](https://discourse.cakephp.org/t/getrequest-in-component-cakeph-3-8/7044/9 "2021-03-12T14:45:51Z")

</div>

You’ve overridden the default component constructor with your own, but not called the parent one.

---

<div class="post-metadata">

**Author:** ![debendra986](https://avatars.discourse-cdn.com/v4/letter/d/c5a1d2/32.png) [@debendra986](https://discourse.cakephp.org/u/debendra986)\
**Post date:** [March 12, 2021, 6:54pm UTC](https://discourse.cakephp.org/t/getrequest-in-component-cakeph-3-8/7044/10 "2021-03-12T18:54:40Z")

</div>

> [@Zuluru](#):
>
> but not called the parent one.

Can you explain it bit more? I’m still not get how to call parent one any example

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [March 12, 2021, 7:37pm UTC](https://discourse.cakephp.org/t/getrequest-in-component-cakeph-3-8/7044/11 "2021-03-12T19:37:22Z")

</div>

It’s not something specific to components. In general, in PHP, when you create a class that extends another class, and you have a ` __construct` function, your function should take all the same parameters as the parent’s `__ construct` function does, and then you do `parent::__construct(...)` with all the parameters in the … spot.
