Welcome Guest, Not a member yet? Register   Sign In
How to start session globally?
#20

This is how I set up sessions on 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\RequestInterface;
use 
CodeIgniter\HTTP\ResponseInterface;
use 
Config\Services;
use 
Psr\Log\LoggerInterface;

/**
 * Class BaseController
 *
 * @package App\Controllers
 */
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 = [
        'auth',
        'utility',
    ];

    /**
     * @var string - Holds the session instance
     */
    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();
        }
    }

}
   // End of Class BaseController.

/**
 * -----------------------------------------------------------------------
 * Filename: BaseController.php
 * Location: ./app/Controllers/BaseController.php
 * -----------------------------------------------------------------------
 */ 

See if that will work for you.

Just call it like this:

PHP Code:
$this->session
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply


Messages In This Thread
How to start session globally? - by happyape - 09-21-2018, 04:35 AM
RE: How to start session globally? - by sv3tli0 - 09-21-2018, 04:59 AM
RE: How to start session globally? - by happyape - 09-21-2018, 05:21 AM
RE: How to start session globally? - by happyape - 09-21-2018, 05:34 AM
RE: How to start session globally? - by sv3tli0 - 09-21-2018, 06:10 AM
RE: How to start session globally? - by happyape - 09-21-2018, 06:16 AM
RE: How to start session globally? - by kilishan - 09-21-2018, 06:16 AM
RE: How to start session globally? - by happyape - 09-21-2018, 06:58 AM
RE: How to start session globally? - by happyape - 09-21-2018, 07:36 AM
RE: How to start session globally? - by macruzgi - 11-04-2019, 10:48 AM
RE: How to start session globally? - by macruzgi - 11-04-2019, 01:21 PM
RE: How to start session globally? - by kilishan - 09-21-2018, 09:21 AM
RE: How to start session globally? - by macruzgi - 11-04-2019, 01:29 PM
RE: How to start session globally? - by macruzgi - 11-04-2019, 03:05 PM
RE: How to start session globally? - by macruzgi - 11-05-2019, 07:51 AM
RE: How to start session globally? - by macruzgi - 11-08-2019, 04:17 PM
RE: How to start session globally? - by InsiteFX - 11-05-2019, 05:50 PM



Theme © iAndrew 2016 - Forum software by © MyBB