Welcome Guest, Not a member yet? Register   Sign In
Initializing a Session
#1

(This post was last modified: 05-17-2020, 01:54 PM by jreklund.)

I'm new to CI and a lot of things involved with it. I'm trying to get a Login Form example working in CI4. I suspect the code is for a previous CI version.

I've read the CI User Guide pages on the Session Library  however am not clear where the code to initialize the session goes. I looked to put it in the autoload.php file as in the example code, however that file looks completely different to the one I have in my AppStarter-initialized app so no idea where to insert it there.

Then I've read that the code should be put in the Controller constructor which gave me errors. Then I read somewhere else that it should be put in a constructor of a class that extends BaseController not Controller. I've only just started using CI and the tutorial Controller classes extended the Controller class so I'm not really sure of the differences between the two.

Anyhow, despite this I tried putting it in the constructor of a Controller that extended BaseController but got the following:

Code:
ErrorException

Declaration of App\Controllers\Login::initController() should be compatible with CodeIgniter\Controller::initController(CodeIgniter\HTTP\RequestInterface $request, CodeIgniter\HTTP\ResponseInterface $response, Psr\Log\LoggerInterface $logger)

Grateful for any help or advice on any of this - thanks!
Reply
#2

You need to add it with the parameters. This are a direct copy from BaseController, you can load the session there. Or place this in another controller that are extending BaseController, but that means you need to extend that controller in order to always have session available.

PHP Code:
    /**
     * 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();
    

Reply
#3

Thank you very much for your help. My code ended up like this:

Code:
use CodeIgniter\Controller;

 
class Login extends Controller {

    /* initController ref: https://forum.codeigniter.com/thread-76492.html */
    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);
        $this->session = \Config\Services::session();
    }

...
...

This worked for me. In other php files of my app I was able to use the $_SESSION superglobal to access session values.

However there are some things I'm missing or don't understand , e.g.:

1) What is the difference between the Controller class and the BaseController class and where is this documented?
2) Why doesn't the CI4 documentation on sessions include the essential information you gave me to initialize a session?

Thanks again!
Reply
#4

(05-19-2020, 07:10 AM)jim1001 Wrote: 1) What is the difference between the Controller class and the BaseController class and where is this documented?

The BaseController documentation is here.

(05-19-2020, 07:10 AM)jim1001 Wrote: 2) Why doesn't the CI4 documentation on sessions include the essential information you gave me to initialize a session?

You want more that this?
Reply
#5

Dave,

Thanks for your help - much appreciated.

(05-19-2020, 08:12 AM)dave friend Wrote: The BaseController documentation is here.

OK, so how do I know what properties and methods the CodeIgniter’s core Controller has that BaseController hasn’t. Does CodeIgniter’s core Controller derive from BaseController?

I initiated a session (code in previous post) using a class that extended CodeIgniter’s core Controller (not BaseController) and this worked. But the docs say

Quote:The base controller is a great place to load any helpers, models, libraries, services, etc. that you intend to use every time your project runs.

So have I done something wrong?

I’ve not done much OO coding for a while but I’m used to seeing classes defined something like this.


(05-19-2020, 08:12 AM)dave friend Wrote: You want more that this?

That says 
Quote:To access and initialize the session:
Code:
$session = \Config\Services::session($config);

But it doesn’t say how to initiate the Session Library which I can now see is included by chance as an example in Preloading Components although it says you should extend BaseController for this.
Reply
#6

(This post was last modified: 05-19-2020, 02:30 PM by dave friend.)

(05-19-2020, 11:04 AM)jim1001 Wrote: OK, so how do I know what properties and methods the CodeIgniter’s core Controller has that BaseController hasn’t. Does CodeIgniter’s core Controller derive from BaseController?

Sometimes you have to read the source code. If you look at the file /app/Controllers/BaseController.php you will see
PHP Code:
class BaseController extends Controller 

The core controller class file is at /system/Controller.php If you examine it you will see that the class has no public properties and this single public method:
PHP Code:
public function initController(RequestInterface $requestResponseInterface $responseLoggerInterface $logger


(05-19-2020, 11:04 AM)jim1001 Wrote: I initiated a session (code in previous post) using a class that extended CodeIgniter’s core Controller (not BaseController) and this worked. But the docs say

Quote:The base controller is a great place to load any helpers, models, libraries, services, etc. that you intend to use every time your project runs.

So have I done something wrong?

Not really wrong, except that you mostly just recreated the DefaultController class which is probably an unnecessary repetition of code.

The purpose of DefaultController is to load code that get used by every controller in your project. If the Session class is used only in the Login controller, then what you did by extending CodeIgniter\Controller makes sense.

(05-19-2020, 11:04 AM)jim1001 Wrote: That says 
Quote:To access and initialize the session:
Code:
$session = \Config\Services::session($config);
But it doesn’t say how to initiate the Session Library

Read the line before the code again.
Quote:To access and initialize the session:

After the code runs the variable $session contains a ready-to-use instance of the Session class.
Reply
#7

Thank you Dave for taking the time and having patience to explain. I know I've a lot to learn and will go through your comments carefully in days to come.  All the best, Jim.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB