• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Sessions, how?

#1
I seem to constantly having a hard time reading and actually USING the docs:

Can someone tell med HOW, WHERE and WHEN to use this:
Code:
$session = \Config\Services::session($config);

or

$session = \Config\Services::session(); // Without the $config


It's the first line of 'code' in the docs about the Session Library (https://codeigniter4.github.io/userguide...ht=session) ... but I cannot – from the docs – figure out HOW to implement and USE it.

If I put it into a Controller, I get this:
Code:
syntax error, unexpected '$session' (T_VARIABLE), expecting function (T_FUNCTION) or const (T_CONST)

Can somone explain why I'm getting this, and what it is that I seem to be constantly missing when 'reading' the docs?

I see this a LOT in the docs '$variable = something … ', but WHENEVER I try to implement what I'm reading in the docs I ALWAYS get errors.

Now, does:

Code:
$variable = ...

in the docs have some 'magical' meaning (should it be 'read' in some particular way)? Or is it simply because the docs 'assume' YOU know where you're supposed to 'put' this code ... and therefore leaving out the (not so) 'obvious'?
Reply

#2
yes i struggle with the docs sometimes; what i do is get concepts working anyway i can then re-visit docs for more elegant ways of doing it.

This is the start of one of my controller's :
Code:
public function credentials()

{
    session_start();
        
    $theArray= $_SESSION['captcha'];

first line of controller (and also any controller) that will pick up the ball will have as first line
session_start();

I set a session array variable using $_SESSION


then later

Code:
public function logout()
              {
               session_start();
                unset($_SESSION['role']);
                unset($_SESSION['count']);
// first line is still session_start();

this is part of a method to unset sessions I use

this is basically old school php ; i'm using it on codeigniter4.0.4


mind you, when i say old school, $_SESSION is not that old since if you look inside SESSION.php file

mine is at vendor/codeigniter4/framework/system/Session/Session.php there are a lot of references and use of $_SESSION
Reply

#3
Looking at your code, it looks like your not declaring the $session variable first. 

Heres a quick example:

PHP Code:
class Controller
{
    protected $session;

    public function __construct()
    {
        $this->session = \Config\Services::session();
    }


Or if you need/want sessions available everywhere, then uncomment the line:
$this->session = \Config\Services:Confusedession();
In the BaseController and extend your controllers off that.

If you search a bit, I believe InSiteFX has a better way to implement them, by checking to see if the session is already active or not, posted here in the forums, just can't remember where.

Hope this helps.
Reply

#4
I start it in the 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 = [];

    
/**
     * @var string
     * Holds the session instance
     */
    
protected $session;

    
/**
     * 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 Services::session();
        }

    }

}
   // End of BaseController Class.

/**
 * -----------------------------------------------------------------------
 * Filename: BaseController.php
 * Location: ./app/Controllers/BaseController.php
 * -----------------------------------------------------------------------
 */ 
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.