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


Messages In This Thread
BaseController __construct - by kristianlake - 01-18-2021, 10:43 AM
RE: BaseController __construct - by InsiteFX - 01-18-2021, 12:32 PM
RE: BaseController __construct - by kristianlake - 01-18-2021, 02:01 PM



Theme © iAndrew 2016 - Forum software by © MyBB