Welcome Guest, Not a member yet? Register   Sign In
Sharing session with no CI app
#3

I have my BaseController like this for checking session.

PHP Code:
<?php

namespace App\Controllers;

use 
CodeIgniter\Controller;
use 
CodeIgniter\HTTP\CLIRequest;
use 
CodeIgniter\HTTP\IncomingRequest;
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
{
    /**
    * Instance of the main Request object.
    *
    * @var CLIRequest|IncomingRequest
    */
    protected $request;

    /**
    * 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 = [];

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

 
/**
 * @var array - Global data array for views.
 *
 * Fill in your global view data here that will be
 * used in all views.
 */
 
protected static $globalViewData = [
 
'test' => 'This works!',
 ];

    /**
    * Constructor.
    */
    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 = \Config\Services::session();
 
    $this->session Services::session();
 
    }
    }

 
/**
 * mergeGlobalData ()
 * -------------------------------------------------------------------
 *
 * You must set the globalViewData array above with your global data
 * for all Controllers.
 *
 * Usage:
 *
 * $data = $this->mergeGlobalData($data);
 * return view('your_view', $data);
 *
 * @param  array $viewData
 * @return array
 */
 
protected function mergeGlobalData(array $viewData) : array
 {
 if (
is_array(self::$globalViewData))
 {
 return 
array_merge($viewDataself::$globalViewData);
 }

 return [];
 }

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

}  // End of BaseController class.

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

Make sure you do the session check to see if it is loaded first.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply


Messages In This Thread
Sharing session with no CI app - by gurthang75 - 12-30-2021, 11:31 AM
RE: Sharing session with no CI app - by ikesela - 12-30-2021, 09:02 PM
RE: Sharing session with no CI app - by InsiteFX - 12-31-2021, 01:35 AM
RE: Sharing session with no CI app - by InsiteFX - 01-04-2022, 02:35 AM



Theme © iAndrew 2016 - Forum software by © MyBB