Welcome Guest, Not a member yet? Register   Sign In
My Library cannot see session
#1

Hi Team, (having resolved helper issue) the debugging show one of my libraries cannot see the session.
I have loaded the library (Template) and the session in the BaseController ie
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 
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.
*/
abstract 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 list<string>
    */
    protected $helpers = ['url''form''functions''time''myvalidate''debug'];

    /**
    * Be sure to declare properties for any property fetch you initialized.
    * The creation of dynamic property is deprecated in PHP 8.2.
    */
    public $session;
public 
$db;
public 
$smail;
public 
$ajax;
public 
$template;
public 
$get;
public 
$make;
public 
$delete;

    /**
    * @return void
    */
    public function initController(RequestInterface $requestResponseInterface $responseLoggerInterface $logger)
    {
        // Do Not Edit This Line
        parent::initController($request$response$logger);

        // Preload any models, libraries, etc, here.

        $this->session = \Config\Services::session();
$this->db = \Config\Database::connect();

$this->smail = new \App\Libraries\Smail();
$this->ajax = new \App\Libraries\Ajax();
$this->template = new \App\Libraries\Template();
$this->get = new \App\Libraries\Get();
$this->make = new \App\Libraries\Make();
$this->delete = new \App\Libraries\Delete();

    }

My controller calls the Template lib which in turn tries to access the session.
I appreciate the libs do not extend BaseController, but I was hoping $this->template and $this->session would be available to everything.
I'm sorry but I appear to be in a morasse of object inheritance and am not sure what to do to fix this?
The error thrown is: Undefined property: App\Libraries\Template::$session
The code snippet from my Template lib is;
PHP Code:
public function render($name$admin false)
    {
        $ip_address real_ip();

        if (!$this->session->get('view_site') || is_ip_blocked($ip_address)) {
            return;
        
Any pointers appreciated, thx, Paul
Reply
#2

(This post was last modified: 05-08-2025, 12:00 PM by michalsn.)

You must either pass a session in your library's constructor or use a helper.

PHP Code:
$this->template = new \App\Libraries\Template($this->session);

class 
Template
{
      public __construct(protected $session)
      {}


or simply:

PHP Code:
public function render($name$admin false)
{
    $ip_address real_ip();

    if (!session()->get('view_site') || is_ip_blocked($ip_address)) {
        return;
    
Reply
#3

You can also use the session helper.
PHP Code:
public function render($name$admin false)
{
    $ip_address real_ip();

    // Use the session helper.
    if (!session('view_site') || is_ip_blocked($ip_address)) {
        return;
    }

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