Home > Net >  Cannot Init Or Declare Session Globally In Codeigniter 4
Cannot Init Or Declare Session Globally In Codeigniter 4

Time:10-01

Why I cannot declare or init session globally in the BaseController.php ?

BaseController.php

<?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.
 */
abstract 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 =   ['BotMenu_helper', 
                            'Channel_helper', 
                            'Day_helper', 
                            'Dir_helper', 
                            'File_helper', 
                            'Key_helper', 
                            'Login_helper', 
                            'Notification_helper', 
                            'Response_helper',
                            'Security_helper',
                            'Ticker_helper',
                            'Utility_helper',
                            'RSS_helper',
                            'User_helper'];
                                

    /**
     * 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();
       
        $this->session = \Config\Services::session();
        $this->language = \Config\Services::language();
        $this->language->setLocale($this->session->lang);
    }
}

When I do post :

$routes->post('ProcessCheckoutApp', 'App\Controllers\Front\AppPayment::ProcessCheckoutApp', ['as' => 'ProcessCheckoutApp']);

I got this error :

message": "CodeIgniter\\Session\\Session and Psr\\Log\\LoggerAwareTrait define the same property ($logger) in the composition of CodeIgniter\\Session\\Session. However, the definition differs and is considered incompatible. Class was composed",

I didn't declare session in any file.

CodePudding user response:

Finally found the problem.

Do not init globally session in the BaseController. Because if you have run a thirdparty autoload.php it will causing the error.

Example that make session error :

public $session;

    function __construct()
    {
        
        require_once APPPATH . 'ThirdParty/vendor/autoload.php';
        $this->session = session();                     
        
    }

The correct way is init the session above the autoload.php

public $session;

    function __construct()
    {
        $this->session = session(); 
        require_once APPPATH . 'ThirdParty/vendor/autoload.php';                    
        
    }
  • Related