Welcome Guest, Not a member yet? Register   Sign In
Call to a member function get() on null Error
#1

Sometimes weird things happens...
I'm developing my web site locally on my PC...things run quite fine...
I have installed Codeigniter Shield ...and now I got the following error:

[Image: basecontroller_error.png]

Any help or hint to fix the issue? Thanks a lot

In my Base Controller I have the following code to have multi language functions:
Code:
public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
    {
        // Do Not Edit This Line
        parent::initController($request, $response, $logger);
        // Preload any models, libraries, etc, here.
        // E.g.: $this->session = service('session');
/**
        * 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();
        }
$config = new App();
        $language = \Config\Services::language();
        $locale = $this->session->get('locale') ?? $this->request->getLocale();
        $language->setLocale($locale);
       
        $this->viewData['locale'] = $locale;
        $this->viewData['supportedLocales'] = $config->supportedLocales;
    }
Reply
#2

To quote the documentation Initializing a Session:
Quote:Sessions will typically run globally with each page load, so the Session class should be magically initialized.
Languages are already also handled by Localization. I don't know why you're reinventing the wheel in your BaseController, but stop it.
Reply
#3

(This post was last modified: Today, 12:49 AM by Vespa.)

Thanks for feedback and hints grimpirate, I really appreciate it.
I don't wanto to reinvent the wheel... I'm trying to add a feature ( just a Selection menu in header page) to allow users to switch among the languages in supportedLocales array.
For what I know ( very poor ) Codeigniter has no such features...but any suggestion on this is appreciated.

I got the issue...
The following code doesn't work anymore, ( weird...it worked fine before Shield installation ):
Code:
if (session_status() == PHP_SESSION_NONE){}
Reply
#4

(This post was last modified: 4 hours ago by grimpirate.)

If what you want is a dropdown that shows values in the language you're using here is some sample code:
PHP Code:
<?php

$options 
= [
    'en'  => lang('Choice.en'),
    'es'    => lang('Choice.es'),
];

echo 
form_dropdown('language'$options); 

Then in your app/Languages you would create subdirectories for each of your languages let's say your supportedLocales was en, es. You would then create a file called Language.php in each directory.

In app/Languages/en/Language.php
PHP Code:
<?php
return [
    'en' => 'English',
    'es' => 'Spanish',
]; 

In app/Languages/es/Language.php
PHP Code:
<?php
return [
    'en' => 'Ingles',
    'es' => 'Español',
]; 

CodeIgniter takes care of determining the language you want displayed (based on user's browser settings) and substitutes accordingly.

Then you submit/validate however you feel appropriate, push into your session, and your code in your BaseController's initController method for setting the language becomes:
PHP Code:
public function initController(RequestInterface $requestResponseInterface $responseLoggerInterface $logger)
    {
        
$request->setLocale(session('language'));
        
// Do Not Edit This Line
        
parent::initController($request$response$logger);

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

        // E.g.: $this->session = service('session');
    

Reply




Theme © iAndrew 2016 - Forum software by © MyBB