05-11-2020, 09:33 PM
(This post was last modified: 05-12-2020, 04:01 AM by hoffmanchan.)
I work on CI4.
When I add "$this->session = Services :: session();", I will be get a error message :
( How to slove it, plz ? )
My BaseController
When I add "$this->session = Services :: session();", I will be get a error message :
( How to slove it, plz ? )
PHP Code:
Fatal error: Uncaught ErrorException: Cannot modify header information - headers already sent by (output started at C:\Users\proj\app\Config\Events.php:27) in 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 error: Uncaught ErrorException: Cannot modify header information - headers already sent by (output started at C:\Users\proj\anaso\app\Config\Events.php:27) in 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');
}
}