Welcome Guest, Not a member yet? Register   Sign In
Codeigniter 4 extend core class process clarification
#1

(This post was last modified: 04-08-2024, 12:10 PM by Semsion.)

An attempt has been made to extend the Codeigniter framework Session class and override function get, instead of replacing the whole Session class /  library. This was by following the guide at https://www.codeigniter.com/user_guide/e...re-classes



Steps taken:



- A new class has been created at `app/Libraries/Session.php`

  - Points of note in this file

    - Added a new namespace: `namespace App\Libraries;`.

    - Pulled in the Codeigniter framework Session class: `use CodeIgniter\Session\Session as BaseSession;`.

    - Extended this class in the function head `class Session extends BaseSession`.

    - Called the parent constructor (not sure if this is required):

      - ```public function __construct()  { parent::__construct($this->driver, $this->config);}```

    - Created a function with the same name `public function get` to override the framework version.

    - As suggested via https://www.codeigniter.com/user_guide/e...re-classes went to the previous section on the same page https://www.codeigniter.com/user_guide/e...re-classes and performed the steps as below:

      - Ensured the Autoload.php could find the namespace App\Libraries (confirmed via `php spark namespaces`).

      - An assumption has been made the interface will be implemented by extending the parent class, that implements the interface.

      - It’s not clear what is meant by `and modify the appropriate Service to load your class in place of the core class.`

        - As adding the below to `app/Config/Services.php` didn’t appear to do anything, another assuption was made that a new Services.php file might need to be created at `app/Libraries/Config/Services.php` (see below also) - as per https://www.codeigniter.com/user_guide/e...re-classes;  however it’s not clear if the content of the class(es) is correct.



PHP Code:
<?php

// app/Config/Services.php

namespace Config;

use 
CodeIgniter\Config\BaseService;

class 
Services extends BaseService
{
    public static function get($getShared true)
    {
        if ($getShared) {
            return static::getSharedInstance('get');
        }
        return new \App\Libraries\Session();
    }


PHP Code:
<?php

// app/Libraries/Config/Services.php

namespace Libraries\Config;

use 
CodeIgniter\Config\BaseService;

class 
Services extends BaseService
{
    public static function get($getShared true)
    {
        if ($getShared) {
            return static::getSharedInstance('get');
        }
        return new \App\Libraries\Session();
    }


- The file located at vendor/codeigniter4/framework/system/Config/Services.php has not been modified.


- `var_dump('Session_get');die;` has been added to  `app/Libraries/Session.php` but is not being hit.


Can anyone possibly assist here please?!
Reply


Messages In This Thread
Codeigniter 4 extend core class process clarification - by Semsion - 04-08-2024, 12:07 PM



Theme © iAndrew 2016 - Forum software by © MyBB