![]() |
Codeigniter 4 extend core class process clarification - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28) +--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30) +--- Thread: Codeigniter 4 extend core class process clarification (/showthread.php?tid=90600) |
Codeigniter 4 extend core class process clarification - Semsion - 04-08-2024 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/extending/core_classes.html#extending-core-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/extending/core_classes.html#extending-core-classes went to the previous section on the same page https://www.codeigniter.com/user_guide/extending/core_classes.html#replacing-core-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/extending/core_classes.html#replacing-core-classes; however it’s not clear if the content of the class(es) is correct. PHP Code: <?php PHP Code: <?php - 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?! RE: Codeigniter 4 extend core class process clarification - kenjis - 04-08-2024 You are confusing. The following code defines a new service named "get". It does not extend Session::get(). PHP Code: // app/Config/Services.php See https://www.codeigniter.com/user_guide/extending/core_classes.html#adding-the-service RE: Codeigniter 4 extend core class process clarification - Semsion - 04-09-2024 Thank you for your response, but the new session file / function still isn't being hit. Please see the changes made below and advise if you meant another way. app/Libraries/Session.php is now just a clone of vendor/codeigniter4/framework/system/Session/Session.php - but with the namespace App\Libraries, and some debug added for testing (a var_dump in the get function, triggered by running vendor/bin/phpunit in the terminal, which calls get() via a unit test). app/Config/Services.php now looks like the below (the forum code formatter appears broken on responses): PHP Code: namespace Config; RE: Codeigniter 4 extend core class process clarification - kenjis - 04-10-2024 It seems you don't get what a method name is in Services class. If you want to replace `session` service, you need to add the session() method in Config\Services. But you added the get() method. It means you added `get` service. RE: Codeigniter 4 extend core class process clarification - Semsion - 04-11-2024 Ok thank you yes; the function name needed changing. After forcing the function parameter to false for $getShared, for testing purposes, an error is returning as: Code: 1) ExampleSessionTest::testSessionSimple This appears to be because of the method call `ini_set('session.save_path', $this->savePath);` in FileHandler.php:72 Do we have any idea how this can be resolved?! RE: Codeigniter 4 extend core class process clarification - kenjis - 04-11-2024 Testing Session is difficult. Because Session does not work in CLI. So if you are not an expert, I recommend you test via web. CI4 provides MockSession for PHPUnit testing. So if you replace Session, you need to create your MockSession, and a feature to use your MockSession in your test case. See https://github.com/codeigniter4/CodeIgniter4/blob/ae1c187b98a680d08a3b07e47b15d35651f0015e/system/Test/CIUnitTestCase.php#L332-L343 RE: Codeigniter 4 extend core class process clarification - Semsion - 04-15-2024 Ok, thank you - will continue to test via the web. This is fine as the main route also creates a session. The current issue is that with the new replacement app/Config/Services.php, which eventually hits `$this->logger->info("Session: Class initialized using '" . $this->config->driver . "' driver.");` inside of vendor/codeigniter4/framework/system/Session/Session.php or it's new replacement (clone) app/Libraries/Session.php - if either of these Session.php files are used (from the new app/Config/Services.php file) (see below) The web page is throwing an error as null: `Call to a member function info() on null`. PHP Code: <?php RE: Codeigniter 4 extend core class process clarification - kenjis - 04-15-2024 The error message tells that $this->logger is null. But if you set Logger, it should not be null. I don't know why you get the error. RE: Codeigniter 4 extend core class process clarification - Semsion - 04-17-2024 Thanks for coming back; the error was because the logger hadn't been set for the session. The error has been resolved now. It's coming across that the session class / method may not be able to be extended / replaced, at least in any reasonable / simple manner. The next error is from vendor/codeigniter4/framework/system/Security/Security.php: TypeError Cannot assign App\Libraries\Session to property CodeIgniter\Security\Security::$session of type ?CodeIgniter\Session\Session PHP Code: SYSTEMPATH/Security/Security.php at line 223 UPDATE: Ok, looks like we're finally there! Have extended app/Libraries/Session.php with CodeIgniterSession, but pulling in at the top of this file `use CodeIgniter\Session\Session as CodeIgniterSession`. This has resolved the previous issue, and we're now able to access this new Session class. |