Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Problem with the config class in a template controller
  • I'm having an issue with getting config settings inside of an extended template controller. To double check this, I just downloaded Fuel 1.8 to test from a fresh code base.

    If I do Config::set('language', 'fr) in the welcome controller's index method and echo \Config::get('language') in the welcome view, it will echo out fr. However, if I extend the welcome controller using Controller_Template and echo \Config::get('language') in my template view, it always defaults to en.

    Any ideas what's causing this? It seems that something in the default template controller is resetting the config setting somewhere.
  • It depends on when what is created and rendered.

    A View object stores the language at the moment of creation, so that you can render views in multiple languages, without being reliant on when exactly the view is rendered.

    If you use Contoller_Template, and you define the template using a property, the template View object is created before the action method is called, so before you change the language.

    There are several options to deal with it, but the best option is

    Controller_Welcome extends Controller_Template
    {
        public function before()
        {
            Config::set('language', 'fr);
            $this->template = \View::forge($this->template);
            parent::before();
        }
    }

    assuming you use the property $template to define the view file to use.
  • Thanks for the response Harro. I put 2 & 2 together that the template view renders the config and lang class calls right away. So the method I'm going about things is probably incorrect.

    I do have a question in regards to extending the template controller. When I call the parent::before() method, should I do it at the end or beginning of my class' before() method? I think this might be the issue as I'm calling the parent::before() method before I run any of my code, therefore rendering is done before any of my code is executed.
  • HarroHarro
    Accepted Answer
    As it is the parent's before method that creates the View, you need to call it after you have created it. If not, you'll just waste CPU by creating the object twice.

    before() doesn't render the template, it just creates the view object, so you can use $this->template->something.

    Fuel only renders after all execution is finished, and succesfully finished. Again, it's poinless to render a View if you have an error for example. 
  • I see where my issue is. I'm able to forge a view to a variable and assign variables to that view afterwards. However, making static calls to config and lang files must be done before calling the view as they cannot be called on the fly afterwards.

    I already figured it out and got it working in my controller. Thanks again for your help. Much appreciated as always.

Howdy, Stranger!

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

In this Discussion