Welcome Guest, Not a member yet? Register   Sign In
Ci4 Base controller vs Controller
#1

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.
Reply
#2

$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'); 
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
/*** NO support in private message - Use the forum! ***/
Reply
#3

(This post was last modified: 10-16-2021, 07:28 PM by kenjis.)

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.
Reply
#4

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




Theme © iAndrew 2016 - Forum software by © MyBB