Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Presenter in base controller works not as expected
  • Have this classes:

    namespace Admin;

    class Base_Admin extends \Base_Controller
    {
        public function before()
        {
            parent::before();
            $this->template->mainmenu = \Presenter::forge('mainmenu');
        }
    }

    ...

    namespace Admin;

    class Controller_Admin extends Base_Admin
    {
        public function before()
        {
            parent::before();
            $this->template->mainmenu->set('active', 2);
        }
       
        public function action_index()
        {
            $this->template->set('content', 'helo world');
        }
    }

    and that Presenter:

    namespace Admin;

    class Presenter_Mainmenu extends \Presenter
    {   
        public function before()
        {
            parent::before();
           
            $this->menu = array(
                0 => array(
                    'title' => 'Dashboard',
                    'uri' => 'admin'
                ),
                1 => array(
                    'title' => 'Pages',
                    'uri' => 'admin/pages'
                ),
                2 => array(
                    'title' => 'Files',
                    'uri' => 'admin/files'
                ),
                3 => array(
                    'title' => 'Users',
                    'uri' => 'admin/users'
                ),
                4 => array(
                    'title' => 'Config',
                    'uri' => 'admin/config'
                )
            );

           $this->active = 0;
       }
    }

    I want to have default value of active property = 0 and I want to have ability to change this as I do it in Controller_Admin:

    $this->template->mainmenu->set('active', 2);

    But what I have? If I set default value then I can't change it anyway! If I don't set default value and not set it in Controller_Admin then I have an error 'Undefined variable: active'. What is wrong in my dicision?
  • Ok. Just I have asked I have found the decision of my problem at the moment )
    I just add this to my Presenter:

    public function view()
    {
        if ( ! isset($this->active)) $this->active = 0;
    }

    But It is strange anyway I can't change value that I before set...
  • HarroHarro
    Accepted Answer
    Fuel uses a model of late rendering for both views and presenters, which means the object is only rendered when it has to be.

    This also means that your Presenter's before is only called when the object is rendered, which is long after your controller code is finished. The $this->active = 0; will therefore overwrite any value you may have set before.

    So your logic is wrong.

Howdy, Stranger!

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

In this Discussion