Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Extending other controllers
  • Hi FuelPHP I am looking at fuelphp and i really love it how its made. But i dont understand "Extending other controllers". I have tried Google it and in the forum, but i could find any thing. Could somebody tell my why you would extend another controller? Thank you!
  • I have ofcause read the docs, but i dont understand why you wanna extend a controller. Why should i do it? It also says that the "under" controllers i going to be safe. It that because they are doing a before command, why would also apply to the extended controllers.
  • It can be useful if you create ( for example ) an admin panel. You can create a base "Controller_Admin" that check if user is logged in, and all other admin controllers extends this controller. Now all admin controllers automatically check if user is logged in.
  • I commonly extend Controller_Template to have both a template and a page layout... Both auto-rendered. It could also be useful as a Public/Private controller set, where front facing controllers extend public and admin extends private...
  • I am familiar with CI's usage of using a base controller but struggling with Fuel implementation. I have managed to create a base.php controller and to extend an existing controller and it works fine. I would be grateful for the script to pass data to the following base.php functions and to return results.
    //======================================
    class Controller_Base extends Controller_Template {
    
    public function action_index($modules=NULL)
    {
       $result = NULL; 
       // do some stuff
       return $result;
    }
    
    public function action_do_something_else($data=NULL)
    {
       $result = NULL; 
       // do some stuff
       return $result;
    }
    }/// endof base.php
    
    
    //=======================================
    class Controller_Modules extends Controller_Base { 
    
    public function action_index($modules=NULL)
    {
       $data = array('apple', 'banana', 'cherry'); 
    
       // following does not work 
       // $result = $this->base->index($data);
       // $result = $this->base->do_something_else($data);
    }
    
    }///endof modules.php
    
    

  • You have to see class extensions as overlays. Because you define an action_index method in Controller_Modules, you can no longer see the method by the same name in the parent class, Controller_Base. You can still call it, using
    parent::action_index();
    

    However, it is not common to do this, unless the method you overload really has the same function, and you are replacing it. If not, use different methods in your base class, so you can call them directly instead of using the 'parent' syntax.
  • Harro Verton wrote on Wednesday 7th of September 2011:
    You have to see class extensions as overlays. Because you define an action_index method in Controller_Modules, you can no longer see the method by the same name in the parent class, Controller_Base. You can still call it, using
    parent::action_index(); However, it is not common to do this, unless the method you overload really has the same function, and you are replacing it. If not, use different methods in your base class, so you can call them directly instead of using the 'parent' syntax.
    Many thanks for the prompt reply. It works fine on my localhost and will try and update online changes and the download tomorrow. >>> You have to see class extensions as overlays.
    Is overlay a standard part of OOP or only the Fuel implementation?
  • John Rutherford wrote on Thursday 8th of September 2011:
    Is overlay a standard part of OOP or only the Fuel implementation?
    This is how class extensions work in PHP. See http://php.net/manual/en/language.oop5.php, in particular the section on "overloading".

Howdy, Stranger!

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

In this Discussion