![]() |
Ci4 Base controller vs Controller - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5) +--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24) +--- Thread: Ci4 Base controller vs Controller (/showthread.php?tid=80307) |
Ci4 Base controller vs Controller - CodingInCodeigniter - 10-15-2021 My apologies for the ambiguous title, I really dont know how to explain my confusion. I thought i was getting somewhere but now im just more lost. I need to have the form helper available globally, I read around and found that i can add it within my base controller which i did like this: Code: public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger) But this resulted in the error "Undefined variable $request". I read the documentation and it says: Quote:An instance of the request class already populated for you if the current class is a descendant of So I changed my controller to extend Controller like this: Code: use App\Models\Admin\AccessModel; If i add this code to my method then Im able to use $something = $request->getVar('foo'); Code: $request = \Config\Services::request(); Im struggling so much migrating to Ci4 and i think it all goes around the core functionality being available from anywhere in the application. Please give me some advise. RE: Ci4 Base controller vs Controller - includebeer - 10-16-2021 $request is a property of the Controller class, so you need to access it with $this->request like any other class property: PHP Code: $foo = $this->request->getPost('foo'); RE: Ci4 Base controller vs Controller - kenjis - 10-16-2021 Your BaseController should be: PHP Code: <?php And your controllers must extend BaseController. RE: Ci4 Base controller vs Controller - ikesela - 10-16-2021 CI4 make thing simple, don't need to extend controller or make variable available globally. since most of its already provided by system already. (except some helper). Helper maybe good but i think better use it when are needed rather than make it available globally. |