• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Session not working in basecontroller

#1
I work on CI4. 

When I add "$this->session = Services :: session();", I will be get a error message :

( How to slove it, plz ? )

PHP Code:
Fatal errorUncaught ErrorExceptionCannot modify header information headers already sent by (output started at C:\Users\proj\app\Config\Events.php:27in C:\Users\proj\anaso\system\Debug\Exceptions.php:162 Stack trace#0 [internal function]: CodeIgniter\Debug\Exceptions->errorHandler(2, 'Cannot modify h...', 'C:\\Users\\proj\\...', 162, Array) #1 C:\Users\proj\anaso\system\Debug\Exceptions.php(162): header('HTTP/1.1 500 In...', true, 500) #2 [internal function]: CodeIgniter\Debug\Exceptions->exceptionHandler(Object(ErrorException)) #3 {main} thrown in C:\Users\proj\anaso\system\Debug\Exceptions.php on line 162

Fatal errorUncaught ErrorExceptionCannot modify header information headers already sent by (output started at C:\Users\proj\anaso\app\Config\Events.php:27in C:\Users\proj\anaso\system\Debug\Exceptions.php:162 Stack trace#0 [internal function]: CodeIgniter\Debug\Exceptions->errorHandler(2, 'Cannot modify h...', 'C:\\Users\\proj\\...', 162, Array) #1 C:\Users\proj\anaso\system\Debug\Exceptions.php(162): header('HTTP/1.1 500 In...', true, 500) #2 C:\Users\proj\anaso\system\Debug\Exceptions.php(223): CodeIgniter\Debug\Exceptions->exceptionHandler(Object(ErrorException)) #3 [internal function]: CodeIgniter\Debug\Exceptions->shutdownHandler() #4 {main} thrown in C:\Users\proj\anaso\system\Debug\Exceptions.php on line 162 

My BaseController 
PHP Code:
<?php
namespace App\Controllers;

/**
 * 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.
 *
 * @package CodeIgniter
 */

use CodeIgniter\Controller;
use 
CodeIgniter\HTTP\IncomingRequest;
use 
Config\Services;
use 
Config\Database;

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 = [];
    protected 
$data = array();
    protected 
$session
    protected 
$request;
    protected 
$db;

    
/**
     * Constructor.
     */
    
public function initController(\CodeIgniter\HTTP\RequestInterface $request, \CodeIgniter\HTTP\ResponseInterface $response, \Psr\Log\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 Services::session();
        }
        
        
// Connect Database
        
$this->db Database::connect();
        
        
// Request Income Data
        
$this->request service('request');        
    }


Reply

#2
$this->session = \Config\Services::session();

u put wrong namespace.
Reply

#3
(05-12-2020, 06:24 AM)nmaa3003 Wrote: $this->session = \Config\Services::session();

u put wrong namespace.

Wrong namespace ? I have put "use Config\Services" on top. 
And I have change my code, it still have the error message. 

Is it something is wrong ?
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.