Ci4 Base controller vs Controller |
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.
$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');
Your BaseController should be:
PHP Code: <?php And your controllers must extend BaseController.
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. |
Welcome Guest, Not a member yet? Register Sign In |