Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Problem with Config::get('language') in Fuel 1.4/master
  • Hi,

    While updating a Fuel 1.3 application to 1.4, I encountered a problem retrieving language with Config class. I work in a multi-lingual environment, so the language in config is set to null.

    Here is the way to reproduce the problem:

    1. In my fuel/app/config/config.php file, I have:
    'language'           => NULL, // Default language

    2. In fuel/app/classes/controller/welcome.php, I have:
    public function action_index()
    {
    \Config::set('language','fr');
    echo 'language'.\Config::get('language');
    return Response::forge(View::forge('welcome/index'));
    }

    3. In fuel/app/views/welcome/index.php, I have:
    </head>
    <body>
    <?php echo 'language'.\Config::get('language'); ?>
    <div id="header">

    The echo in the controller (point 2) successfully displays the language that has been set while the echo in the view (point 3) doesn't display anything.
    This happens only with language, other configs seems to work fine.

    Does anyone have an idea how to fix that ?

    Thanks for reading,
  • HarroHarro
    Accepted Answer
    Did some debugging.

    This is caused by the fact that a request now stores and resets the language. Problem is that views are by default late-rendered, only after the request has been completed. At that time, the Config (which is global) no longer reflect the state of the config at the time of the request.

    Can you add an issue for this to the bugtracker at http://github.com/fuel/core/issues so it can be fixed?
  • Hi Harro,

    Thanks for your answer, I'll post an issue.

    For clarification, here is the functional behaviour I'm expecting :
    1. A main request is called and will define a language for the very first time (e.g. based on URL first segment, browser, session language settings);
    2. The main request launches sub-requests that will by default use the language set up of the main request. Some of these sub-requests might switch to another language if they needed to.

    The problem I see in the management of language in fuel/core/classes/request.php with the execute() method is that for this very first request, there is no language previously defined (the config language being set to null). So, at the end of the execute method, the language is set back to… null.

    Someone suggested to me to set the language before this first request instead of inside it. This seems to be an interesting solution. I might do that in bootstrap.php. What do you think about that ? Should that be the good way to manage language when it is defined to null in the config file ?


  • Ideally you should set your default language in the config file, instead of using NULL. But that still causes problems if you want to switch language.

    Problem is that language is a request context parameter, but when Views or Viewmodels are lazy rendered (and this is the default), the code in them will only be executed long after the request is finished, so that context is no longer available.
  • OK. I actually did render the view in the main request and everything works fine this way !
  • This is indeed the current workaround.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion