Welcome Guest, Not a member yet? Register   Sign In
CI4: not getting session data in another controller
#1

Dear Sir,

In Auth Controller, I have set session data and redirect to Homepage controller but I am unable to get session data in Homepage controller. Please find below code.

In Auth.php (Controller)

$mySess = session();
                    $mySess->set($data);         
//                    $this->session->set($data);

                    return redirect()->to(site_url("homepage"));


In Homepage (Controller)

<?php
namespace App\Controllers;
use CodeIgniter\Controller;


class Homepage extends BaseController
{

    public function index() {
        $mySess = session();
        $sessionArray = $mySess->get();
        print_r($sessionArray);
        exit();
    }
//--------------------------------------------------------------------
}



as a result I am getting blank page.

Please help.
Reply
#2

I've the same problem
can you tell me how you solved it?
Reply
#3

Once you load the session it is loaded for all controllers etc;

Just use.


PHP Code:
// using the helper
$value session()->get('value'); 

I load the session library in the Base controller then it is global to all.

PHP Code:
<?php

namespace App\Controllers;

use 
CodeIgniter\Config\Services;
use 
CodeIgniter\Controller;
use 
CodeIgniter\HTTP\CLIRequest;
use 
CodeIgniter\HTTP\IncomingRequest;
use 
CodeIgniter\HTTP\RequestInterface;
use 
CodeIgniter\HTTP\ResponseInterface;
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 IncomingRequest|CLIRequest
     */
    
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 = [
        
'debug',
    ];

    
/**
     * @var \CodeIgniter\Session\Session
     */
    
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();
        }

    }



I then use the helper with chaining to do what I need but you can call it using

PHP Code:
$this-session->get('item'); 
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply




Theme © iAndrew 2016 - Forum software by © MyBB