CodeIgniter Forums
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)
    {
        // Do Not Edit This Line
        parent::initController($request, $response, $logger);

        // Preload any models, libraries, etc, here.
        helper('form');

        // E.g.: $this->session = \Config\Services::session();
        $this->session = \Config\Services::session();
    }
Moving along my application i then needed to start saving values to the database and unlike Ci3 where you could use $this->input->post('name'), i tried using $something = $request->getVar('foo');

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
Code:
CodeIgniter\Controller
and can be accessed as a class property:

So I changed my controller to extend Controller like this:

Code:
use App\Models\Admin\AccessModel;
use CodeIgniter\Controller;
class Access extends Controller
{
I still get this error: ErrorException Undefined variable $request and  helper('form') is not available.

If i add this code to my method then Im able to use $something = $request->getVar('foo');
Code:
$request = \Config\Services::request();
 But do I really need to use this everywhere, i feel like it is so much more work that autoloading the database as is Ci3?

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
...

class 
BaseController extends Controller
{
    ...

    /**
    * An array of helpers to be loaded automatically upon
    * class instantiation. These helpers will be available
    * to all other controllers that extend BaseController.
    *
    * @var array
    */
    protected $helpers = ['form'];

    ....


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.