codeigniter 4 set session variable $session = \Config\Services::session(); globally
Asked Answered
G

4

6

in codeigniter 4 as per the documentation we have to load library as

$session = \Config\Services::session();

if i write it on above of controller name

<?php
namespace App\Controllers;

    $session = \Config\Services::session();

    class Home extends BaseController
    {

    }

i cant access $session variable in any function, even if i wrote it in __construct it is not accessible in any function if i write it any function then only it is working, but i wants to set it globally.

Galvez answered 29/4, 2020 at 10:19 Comment(0)
J
5

Initiate the session globally on the Base Controller in the "initController" function

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

Use it anywhere

$this->session->set('title','value');
Jehanna answered 12/5, 2021 at 8:15 Comment(0)
G
2

i solved it, as below

<?php
namespace App\Controllers;




class Home extends BaseController
{

    protected $session;


    function __construct()
    {

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


    }



    public function code4()
    {

        $newdata = [
                'username'  => 'johndoe',
                'email'     => '[email protected]',
                'logged_in' => TRUE
        ];

        $this->session->set($newdata); // setting session data


        echo $this->session->get("username");

    }

    //--------------------------------------------------------------------

}
Galvez answered 29/4, 2020 at 12:23 Comment(3)
Yep that will do it. You don't need to call the session start. WHEN you try to call session vars in your views - use the Common.php "helper" session(); read up on it.Chyack
@Chyack do you have any reference to the Common.php "helper" session() you mention? I didn't get anything helpful back from Google. Thank you!Chandlery
@Chandlery you can actully find the code and comments in the actual file itself.Chyack
B
2

You can use the function session() anywhere you want. It is a convenience method for accessing the session instance or an item that has been set in the session.

session(); // instantiate the session class

If you don't pass a string argument it returns a session class instance. It will return a shared instance if it exists or return a newly created instance if it does not.

Once a session is instantiated using the superglobal $_SESSION is the most efficient way to set session data.

$_SESSION['username']  = 'johndoe';
$_SESSION['email']     = '[email protected]';
$_SESSION['logged_in'] = TRUE;

Pass a string argument to retrieve saved session data

$userName = $session('username');

Or, you can use Session methods like this.

$session = session();
$userName = $session->get('username');

You don't have to load a "helper" in order to use session() is always loaded and available.

Belfast answered 30/4, 2020 at 14:50 Comment(1)
Thanks. The middle part of your coding is a little off the track. But The first and the last part is awesome. It worked like magic and now I don't have to use the session variable and class again and againWare
W
0

Another solution is use like this:

`session()->get('name');// With this you can use any session value anywhere in the application `
Ware answered 2/3, 2021 at 7:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.