-
includebeer
CodeIgniter Team
-
Posts: 1,018
Threads: 18
Joined: Oct 2014
Reputation:
40
(10-29-2021, 01:13 PM)chronic Wrote: I have seen that if I change the value in the "system\HTTP\IncomingRequest.php" file constructor:
from
PHP Code: $this->validLocales = $config->supportedLocales;
to
PHP Code: $this->validLocales = service('settings')->get('App.supportedLocales');
then it works fine as I expect, but I would like to avoid modifying the files under system folder.
Yeah, don't do that. Changing system files is the worst solution. Look at this page, you should be able to extend the IncomingRequest class and override the constructor.
-
chronic
Junior Member
-
Posts: 34
Threads: 4
Joined: Nov 2014
Reputation:
0
Very interesting, that's probably what I need, tomorrow I'll read better and see what I can do.
Thanks
-
InsiteFX
Super Moderator
-
Posts: 6,507
Threads: 323
Joined: Oct 2014
Reputation:
239
It ignores it because the app/Config/Config.php is injected into and loaded by CodeIgniter in the begining.
CodeIgniter.php
PHP Code: /** * Constructor. */ public function __construct(App $config) { if (version_compare(PHP_VERSION, self::MIN_PHP_VERSION, '<')) { // @codeCoverageIgnoreStart $message = extension_loaded('intl') ? lang('Core.invalidPhpVersion', [self::MIN_PHP_VERSION, PHP_VERSION]) : sprintf('Your PHP version must be %s or higher to run CodeIgniter. Current version: %s', self::MIN_PHP_VERSION, PHP_VERSION);
exit($message); // @codeCoverageIgnoreEnd }
$this->startTime = microtime(true); $this->config = $config; }
Now you see why.
What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
-
chronic
Junior Member
-
Posts: 34
Threads: 4
Joined: Nov 2014
Reputation:
0
(10-30-2021, 06:01 AM)includebeer Wrote: (10-29-2021, 01:13 PM)chronic Wrote: I have seen that if I change the value in the "system\HTTP\IncomingRequest.php" file constructor:
from
PHP Code: $this->validLocales = $config->supportedLocales;
to
PHP Code: $this->validLocales = service('settings')->get('App.supportedLocales');
then it works fine as I expect, but I would like to avoid modifying the files under system folder.
Yeah, don't do that. Changing system files is the worst solution. Look at this page, you should be able to extend the IncomingRequest class and override the constructor.
I tried to play a bit with the code of the page you suggested, but I couldn't find a solution, maybe it's a bit too much for my abilities.
-
includebeer
CodeIgniter Team
-
Posts: 1,018
Threads: 18
Joined: Oct 2014
Reputation:
40
(10-31-2021, 09:15 AM)chronic Wrote: I tried to play a bit with the code of the page you suggested, but I couldn't find a solution, maybe it's a bit too much for my abilities.
I didn't test it, but I think all you have to do is define a new library in your app that extends the core class:
PHP Code: <?php namespace App\Libraries;
use CodeIgniter\HTTP\IncomingRequest as BaseIncomingRequest;
class IncomingRequest extends BaseIncomingRequest { public function __construct($config, ?URI $uri = null, $body = 'php://input', ?UserAgent $userAgent = null) { parent::__construct($config, $uri, $body, $userAgent); // your code here $this->validLocales = service('settings')->get('App.supportedLocales'); } }
Then add a request() service in app/Config/Services.php that will load your library. You can copy the one in System/Config/Services.php and add a "use" statement to use your library:
PHP Code: use App\Libraries\IncomingRequest;
/** * The Request class models an HTTP request. * * @return IncomingRequest */ public static function request(?App $config = null, bool $getShared = true) { if ($getShared) { return static::getSharedInstance('request', $config); }
$config = $config ?? config('App');
return new IncomingRequest( $config, AppServices::uri(), 'php://input', new UserAgent() ); }
Like I said, I didn't test this code, but it should be close to what you need to do.
-
chronic
Junior Member
-
Posts: 34
Threads: 4
Joined: Nov 2014
Reputation:
0
11-01-2021, 09:23 AM
(This post was last modified: 11-01-2021, 09:24 AM by chronic.)
Thanks includebeer, after some changes to your code I was able to get the result I wanted.
Everything seems to work as it should, I hope I have not left out something important and that this does not create problems with future versions of codeigniter.
app\Libraries\IncomingRequest.php
PHP Code: <?php
namespace App\Libraries;
use CodeIgniter\HTTP\IncomingRequest as BaseIncomingRequest;
class IncomingRequest extends BaseIncomingRequest { public function __construct($config, $uri = null, $body = 'php://input', $userAgent = null) { parent::__construct($config, $uri, $body, $userAgent);
// your code here $this->validLocales = service('settings')->get('App.supportedLocales'); } }
app\Config\Services.php
PHP Code: <?php
namespace Config;
use CodeIgniter\Config\BaseService; use App\Libraries\IncomingRequest; use CodeIgniter\HTTP\UserAgent; use Config\App; use Config\Services as AppServices;
/** * Services Configuration file. * * Services are simply other classes/libraries that the system uses * to do its job. This is used by CodeIgniter to allow the core of the * framework to be swapped out easily without affecting the usage within * the rest of your application. * * This file holds any application-specific services, or service overrides * that you might need. An example has been included with the general * method format you should use for your service methods. For more examples, * see the core Services file at system/Config/Services.php. */ class Services extends BaseService { // public static function example($getShared = true) // { // if ($getShared) // { // return static::getSharedInstance('example'); // } // // return new \CodeIgniter\Example(); // }
/** * The Request class models an HTTP request. * * @return IncomingRequest */ public static function request(?App $config = null, bool $getShared = true) { if ($getShared) { return static::getSharedInstance('request', $config); }
$config = $config ?? config('App');
return new IncomingRequest( $config, AppServices::uri(), 'php://input', new UserAgent() ); } }
-
includebeer
CodeIgniter Team
-
Posts: 1,018
Threads: 18
Joined: Oct 2014
Reputation:
40
(11-01-2021, 09:23 AM)chronic Wrote: Everything seems to work as it should, I hope I have not left out something important and that this does not create problems with future versions of codeigniter.
If the contructor change in future versions, you'll need to adjust your library and service, but it shouldn't be that big of a change.
I'm happy it's working now!
|