CodeIgniter Forums
Accessing "$request" and "validate" from another class - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: Accessing "$request" and "validate" from another class (/showthread.php?tid=75056)



Accessing "$request" and "validate" from another class - webdevron - 12-15-2019

I have a subfolder inside controller app/Controller/AppManager. Inside it there is a class named "MediaManager.php" which is extended with use CodeIgniter\Controller;.


PHP Code:
<?php

namespace App\Controllers\AppManager;
use 
CodeIgniter\Controller;

class 
MediaManager extends Controller
{
    public function uploadFile(){
        $file $this->request->getFile('file');
        $fileRule = ['file' => 'uploaded[file]|max_size[file,20480]'];

        if( ! $this->validate($fileRule) ) $ret = ['e''FAILED'$this->validator->listErrors()];
        ...
        ...
        ...
    }



Now the problem is, when ever I am going to upload a file, these errors are showing:
  • Call to a member function getFile() on null
  • Argument 1 passed to CodeIgniter\Validation\Validation::withRequest() must implement interface CodeIgniter\HTTP\RequestInterface, null given
How can I access the Request and Validation property in this class?


RE: Accessing "$request" and "validate" from another class - MGatner - 12-15-2019

Those libraries are initialized on the current routed controller during initController, so if you are loading the controller from somewhere else you will need to inject those libraries from the current controller. If you are extending that controller to access those methods it should work fine. Can you share your code that is trying to make the uploadFile() call?


RE: Accessing "$request" and "validate" from another class - InsiteFX - 12-15-2019

See the CodeIgniter 4 User Guide.

Accessing the Request


RE: Accessing "$request" and "validate" from another class - webdevron - 12-15-2019

(12-15-2019, 05:00 AM)MGatner Wrote: Those libraries are initialized on the current routed controller during initController, so if you are loading the controller from somewhere else you will need to inject those libraries from the current controller. If you are extending that controller to access those methods it should work fine. Can you share your code that is trying to make the uploadFile() call?

Those libraries are initialized on the current routed controller during initController - this was the main problem. Now I get it. Thank you.

(12-15-2019, 05:33 AM)InsiteFX Wrote: See the CodeIgniter 4 User Guide.

Accessing the Request

Thank you @InsiteFX. I read this part of the user guide just before posting the thread, but could not get it then. Now it is clear and solved my issue.