![]() |
I'm seeing more questions about multilanguage and people using sessions to mess about with them. I want to point out that there is never a need to check if the session has been initialized. By calling the service, if a session doesn't exist, it will be created. In other words this code is unneeded:
PHP Code: if (session_status() == PHP_SESSION_NONE) { You can always access the session by calling session() helper (the only exception to this is if you need some alternative configuration from the default). Doing $this->session offers no benefit as session returns the shared instance of the session, you're just creating more memory pointers. However, if you insist on doing so there's no need for the if statement. You only require the following (and can proceed to access the session doing $this->session): PHP Code: $this->session = session(); The language library is also loaded automatically and your browser (usually based on operating system settings) is preconfigured in whichever language you natively use. Modern browsers sends an 'Accept-Language' header with their requests (there are other ways as well) to let the server know which language it wishes to see. CodeIgniter can detect this by setting up automatic Content Negotiation. So setting up Content Negotiation to automatic removes this code: PHP Code: $lang = service('language'); Custom code should only be needed in the (albeit illogical) case where a user wants to see a different language than what his system is configured in. In this case you would modify the request to get the locale from the session data (which would have been actualized via a language selection somewhere on the site) rather than through content negotiation. I'm only calling this out here (as I've seen this code on another post recently) because this is the "Best Practices" section. CodeIgniter 4 has features which makes these particular things a breeze. Read every page of the docs (there isn't a single one that is superfluous), use the features. |
Messages In This Thread |
Auth Shield and multilanguage - by kaziul - 06-05-2025, 12:37 AM
RE: Auth Shield and multilanguage - by InsiteFX - 06-05-2025, 11:12 PM
RE: Auth Shield and multilanguage - by kaziul - 06-07-2025, 05:34 AM
RE: Auth Shield and multilanguage - by grimpirate - 06-10-2025, 03:03 PM
RE: Auth Shield and multilanguage - by InsiteFX - Yesterday, 02:48 AM
RE: Auth Shield and multilanguage - by grimpirate - Yesterday, 09:01 AM
RE: Auth Shield and multilanguage - by InsiteFX - Yesterday, 09:21 PM
|