Welcome Guest, Not a member yet? Register   Sign In
BaseController __construct
#1

Hi all, 

Been using CI4 for a while now and my project is really nicely established and going really well.

I wanted to check something that's been bothering me for a while though, As I cant find any information on this.


I have a base controller (code stripped down)

<?php
namespace App\Controllers;

class BaseController extends Controller
{

    protected $session;
    protected $helpers = [];
    protected $data = [];


public function __construct()
{
    // start session
    if ($this->session == null) {
        log_message('debug', 'BaseController In __construct starting session service');
        $this->session = Services::session();
    }

}

I have several sub controllers all coming off this

<?php

namespace App\Controllers;
class Dashboard extends BaseController
{

   ...
}


When visiting my sub controllers my logging shows as such

DEBUG - 2021-01-18 17:27:50 --> BaseController In __construct starting session service
INFO - 2021-01-18 17:27:50 --> Session: Class initialized using 'CodeIgniter\Session\Handlers\FileHandler' driver.

.

And whenever I call a route via url it always runs everything in __construct

My question is, Is this normal behavior? Do I need to create a session service for every call to the controllers? Or Should I be able to start a session once (perhaps with a time duration) and have it use the same session?

Thanks in advance guys!
Reply
#2

This is how I do my BaseController.

PHP Code:
<?php

namespace App\Controllers;

use 
CodeIgniter\Controller;
use 
CodeIgniter\HTTP\RequestInterface;
use 
CodeIgniter\HTTP\ResponseInterface;
use 
Config\Services;
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
{
    
/**
     * 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 = [
        
'debug',
        
'utility',
        
'session',
        
'filesystem',
    ];

    
/**
     * @var \CodeIgniter\Session\Session
     */
    
protected $session;

    
/**
     * Constructor.
     *
     * @param RequestInterface  $request
     * @param ResponseInterface $response
     * @param LoggerInterface   $logger
     */
    
public function initController(RequestInterface $requestResponseInterface $responseLoggerInterface $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();
        }

    }
   // ---------------------------------------------------------------



Also you can always call the session helper.

PHP Code:
$item session('itemName');

// it is also chainable 
$item session()->get('itemName'); 
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

(This post was last modified: 01-18-2021, 02:03 PM by kristianlake.)

thanks, thats really interesting. Im going to try that session_status() == PHP_SESSION_NONE.

Still seems to always spin a session.

Surely if I just made a request my session shouldn't die once I get a response?
Reply




Theme © iAndrew 2016 - Forum software by © MyBB