Welcome Guest, Not a member yet? Register   Sign In
[Solved] Initialising Session with BaseController
#1

(This post was last modified: 06-17-2024, 12:15 AM by jmuhwezi.)

I'm trying to initialise Sessions in the BaseController but I'm getting error Attempt to read property "driver" on null
My Config\app variables

public $sessionDriver = 'CodeIgniter\Session\Handlers\FileHandler';
public $sessionCookieName = 'ci_session';
public $sessionExpiration = 7200;
public $sessionSavePath = WRITEPATH . 'session';
public $sessionTimeToUpdate = 300;

My Base class
Code:
<?php

namespace App\Controllers;

use CodeIgniter\Controller;
use CodeIgniter\HTTP\CLIRequest;
use CodeIgniter\HTTP\IncomingRequest;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use Psr\Log\LoggerInterface;

/**
* Class BaseController
*
* BaseController provides a convenient place for loading components
* and performing functions that are needed by all your controllers.
* Extend this class in any new controllers:
*    class Home extends BaseController
*
* For security be sure to declare any new methods as protected or private.
*/
class BaseController extends Controller
{
    /**
    * Instance of the main Request object.
    *
    * @var CLIRequest|IncomingRequest
    */
    protected $request;

    /**
    * 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 = [];

    /**
    * @var string
    * Holds the session instance
    */
    protected $session;

    /**
    * Constructor.
    */
    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.

        // E.g.: $this->session = \Config\Services::session();
       
      // Ensure that the session is started and running
      if (session_status() == PHP_SESSION_NONE)
      {
          $this->session = \Config\Services::session();
      }
    }
}
Reply
#2

(This post was last modified: 06-16-2024, 07:42 PM by kenjis.)

Here is my Config\Service class
Code:
<?php

namespace Config;

use CodeIgniter\Config\BaseService;

/**
* Services Configuration file.
*
* Services are simply other classes/libraries that the system uses
* to do its job. This is used by CodeIgniter to allow the core of the
* framework to be swapped out easily without affecting the usage within
* the rest of your application.
*
* This file holds any application-specific services, or service overrides
* that you might need. An example has been included with the general
* method format you should use for your service methods. For more examples,
* see the core Services file at system/Config/Services.php.
*/
class Services extends BaseService
{
    /*
    * public static function example($getShared = true)
    * {
    *    if ($getShared) {
    *        return static::getSharedInstance('example');
    *    }
    *
    *    return new \CodeIgniter\Example();
    * }
    */
}

I don't have Config\Session, Could be the reason I'm getting the above error? And if yes, what do I need to put in the file.
Reply
#3

(06-16-2024, 05:37 AM)jmuhwezi Wrote: My Config\app variables

public $sessionDriver = 'CodeIgniter\Session\Handlers\FileHandler';
public $sessionCookieName = 'ci_session';
public $sessionExpiration = 7200;
public $sessionSavePath = WRITEPATH . 'session';
public $sessionTimeToUpdate = 300;

It seems your Config files are outdated.
Now there is no $session* properties in Config\App.

You need to set up "app/Config/Session.php".
https://github.com/codeigniter4/CodeIgni...ession.php
Reply
#4

(This post was last modified: 06-16-2024, 08:15 PM by jmuhwezi.)

thanks @kenjis you right I have outdated files. When I added the Session.php file, it created another error: foreach() argument must be of type array|object, string given, which seem to be in the service file around the getIpAddress.

can you guide on the same as well?

Code:
ErrorException
foreach() argument must be of type array|object, string given


Code:
SYSTEMPATH/HTTP/RequestTrait.php at line 87

Code:
SYSTEMPATH/HTTP/RequestTrait.php : 87  —  CodeIgniter\Debug\Exceptions->errorHandler ( arguments )

SYSTEMPATH/Config/Services.php : 688  —  CodeIgniter\HTTP\Request->getIPAddress ()

SYSTEMPATH/Config/BaseService.php : 311  —  CodeIgniter\Config\Services::session ( arguments )
Reply
#5

It is because your Config\App is still outofdated.
Update $proxyIPs value.
https://github.com/codeigniter4/CodeIgni...p.php#L183

If you are using git, I recommend you copy all the latest Config/*.php file into your project, and check the differences and apply updates in items that you did not change.
Reply
#6

Yes I'm using git @kenjis, and I have copied the Config\app file as guided. Do I still need to initialise session in BaseController file 

Code:
$this->session = \Config\Services::session();
because seems to be re-initialising the session and giving the following error;

Code:
ErrorException
ini_set(): Session ini settings cannot be changed after headers have already been sent
Code:
SYSTEMPATH/Session/Handlers/FileHandler.php at line 74
Code:
{PHP internal code}  —  CodeIgniter\Debug\Exceptions->errorHandler ( arguments )
SYSTEMPATH/Session/Handlers/FileHandler.php : 74  —  ini_set()
SYSTEMPATH/Config/Services.php : 688  —  CodeIgniter\Session\Handlers\FileHandler->__construct ( arguments )

Reply
#7

(06-16-2024, 09:02 PM)jmuhwezi Wrote: ini_set(): Session ini settings cannot be changed after headers have already been sent

As the error message says, your app has already sent something before the call of \Config\Services:: session().
Remove the code to output something. It might be a white space or new line code or BOM (Byte Order Mark) in somewhere that is not needed.

But the error should not happen if you write clean code.
Try to install clean CI4 and add the code in BaseController, and check.
You would probably see no error.
Reply
#8

Thanks Resolved
Reply




Theme © iAndrew 2016 - Forum software by © MyBB