Welcome Guest, Not a member yet? Register   Sign In
Problem with CI4 session
#5

(This post was last modified: 12-19-2019, 09:17 AM by InsiteFX.)

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
{
    /**
     * @var array - For view data
     */
    protected $data = array(); // parameters for view components

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

    /**
     * @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
 * -----------------------------------------------------------------------
 */ 

Create new Test Controller:

PHP Code:
<?php namespace App\Controllers;

/**
 * Class Test
 *
 * @package App\Controllers
 */
class Test extends BaseController
{
    /**
     * Class properties go here.
     * -------------------------------------------------------------------
     * public, private, protected, static and const.
     */


    /**
     * __construct ()
     * --------------------------------------------------------------------
     *
     * Constructor
     *
     * NOTE: Not needed if not setting values or extending a Class.
     *
     */
    public function __construct()
    {

    }

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

    /**
     * index ()
     * -------------------------------------------------------------------
     *
     */
    public function index()
    {
        $newdata = [
            'username'  => 'johndoe',
            'email'     => '[email protected]',
            'logged_in' => TRUE
        
];

        session()->set($newdata);

        return view('welcome_message');
    }

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

}   // End of Test Controller Class.

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

Add this to the bottom of Views welcome_message.php

PHP Code:
<p>
 
  <?= session('username');?>
   <?= session('email');?>
   <?= session('Logged_in');?>
</p> 

You will see that sessions do work.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply


Messages In This Thread
Problem with CI4 session - by webdevron - 12-15-2019, 12:25 AM
RE: Problem with CI4 session - by InsiteFX - 12-15-2019, 05:38 AM
RE: Problem with CI4 session - by webdevron - 12-15-2019, 02:00 PM
RE: Problem with CI4 session - by InsiteFX - 12-16-2019, 06:20 AM
RE: Problem with CI4 session - by InsiteFX - 12-16-2019, 09:17 AM
RE: Problem with CI4 session - by asepma - 04-24-2020, 04:19 AM
RE: Problem with CI4 session - by InsiteFX - 04-24-2020, 08:17 AM



Theme © iAndrew 2016 - Forum software by © MyBB