无法在Codeigniter 4中全局初始化或声明会话

hfsqlsce  于 2022-12-06  发布在  其他
关注(0)|答案(1)|浏览(147)

为什么我不能在BaseController.php中全局声明或初始化会话?

基本控制器.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);
    }
}

当我发布:

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

我得到这个错误:

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",

我没有在任何文件中声明会话。

roqulrg3

roqulrg31#

终于发现了问题所在。
不要在BaseController中全局初始化会话。因为如果你运行了第三方的autoload.php,它将导致错误。

导致会话错误示例:

public $session;

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

正确的方法是在autoload.php上面初始化会话

public $session;

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

相关问题