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

(This post was last modified: 12-15-2019, 12:25 AM by webdevron.)

Do not know why, the session in the constructor is not working but in other method. Help me to find this out. Thanks in advance.

PHP Code:
<?php

namespace App\Controllers;

class 
Manage extends BaseController
{

 protected 
$session;

 public function 
__construct(){
     
$this->session = \Config\Services::session();
     if( !
$this->session->has('isCnProL') ) return redirect()->to(base_url('login')); // Not working
 
}

 public function 
new_post()
 {
     if( !
$this->session->has('isCnProL') ) return redirect()->to(base_url('login')); // Working
     
$this->manageView('post-new');
 } 
"Who thinks in code"
Perfectly describes who I am
mbparvez.me
Reply
#2

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

I load my session in the BaseController like below and it works just fine.

Add a:

PHP Code:
use Config\Services


PHP Code:
// Ensure that the session is started and running
if (session_status() == PHP_SESSION_NONE)
{
    $this->session Services::session();


By the way I use the session files driver and save my sessions to the writable folder.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

(This post was last modified: 12-15-2019, 02:04 PM by webdevron.)

Now I am loading session in my BaseController.

PHP Code:
<?php

namespace App\Controllers;
use 
CodeIgniter\Controller;

class 
BaseController extends Controller
{
    protected 
$session;

    public function 
initController(\CodeIgniter\HTTP\RequestInterface $request, \CodeIgniter\HTTP\ResponseInterface $response, \Psr\Log\LoggerInterface $logger)
    {
        
parent::initController($request$response$logger);
        if (
session_status() == PHP_SESSION_NONE)
        {
            
$this->session = new Config\Services::session();
        } 
    }



Then in my Controller. But still the same result.

PHP Code:
<?php

namespace App\Controllers;

class 
Manage extends BaseController
{
    public function 
initController(\CodeIgniter\HTTP\RequestInterface $request, \CodeIgniter\HTTP\ResponseInterface $response, \Psr\Log\LoggerInterface $logger){
        
parent::initController($request$response$logger);
        if( !
$this->session->has('isCnProL') ) return redirect()->to(base_url('login')); // NOT WORKING
    
}

    public function 
new_post()
    {
    
    if( !$this->session->has('isCnProL') ) return redirect()->to(base_url('login')); // THIS IS WORKING
    
    $this->manageView('post-new');
    }

"Who thinks in code"
Perfectly describes who I am
mbparvez.me
Reply
#4

You do not need the initController in a regular controller it is inherited from the
BaseController, as long as you extend your controllers from BaseController.

Just load your session in the BaseController then access the session in any method.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#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
#6

please help.

i have a libary code like bellow

libarary

<?php namespace App\Libraries\Mylibrary;

class Mylibrary{

function hitung(){
$angka = 2;
return $angka;
}
}

controller :

<?php namespace App\Controllers;

use App\Libraries\Mylibrary;

class Home extends BaseController
{

function sesi(){

$this->session->set('abc','item');
if ($this->session->get('abc')!='item') {
echo "string";
}else{
echo "strong";
}

echo hitung();
}
}

the error

Call to undefined function App\Controllers\hitung()

thanks.
Reply
#7

(This post was last modified: 04-24-2020, 08:18 AM by InsiteFX.)

PHP Code:
$lib = new Mylibrary();

echo 
$lib->hitung(); 

Or autoload the library.
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