Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
CI-like MY_Controller
  • Hi, I'm trying to make the CI "MY_Controller" idea in FuelPHP. What I want is this. All the controllers for normal webpages (e.g. de pages for visitors) have to extend base controller like "Controller_Public". I've created a file public.php in /fuel/app/classes/controllers/ en I've placed home.php in /fuel/app/classes/controllers/public Controller_Public:
    class Controller_Public extends Controller {
    
    }
    

    Home.php
    <?php
    
    class Controller_Home extends Controller_Template {
     
     public $template = 'layout/index';
     
     public function __construct(\Request $request, \Response $response)
     {
      parent::__construct($request, $response);
     }
     
     public function action_index()
     {
      $data['title'] = 'Peter';
      
      $this->template->content = View::factory(Request::active()->controller.'/'.Request::active()->action, $data);
     }
     
    }
    
    

    But that isn't working. Is my approach wrong? My routes: '_root_' => 'home/index', // The default route
    '_404_' => 'home/404', // The main 404 route
  • Your base public controller needs to extend the Controller_Template and your home controller needs to extend your public controller. Public
    class Controller_Public extends Controller_Template&#123;
    
    }
    

    Home
    class Controller_Home extends Controller_Public&#123;
    
    }
    
  • If you want to create multi-level base controllers, just make sure you use parent::before() function class Controller_Home extends Sell_Base { public function action_index()
    {
    { } class Base extends Site_Base { public function before()
    {
    parent::before();
    } } class Admin_Base extends Site_Base { public function before()
    {
    parent::before();
    } } class Site_Base extends Controller_Template { public function before()
    {
    parent::before();
    { }
  • Thanks Calvin and Huzzi, that did the trick. But now I'm trying to load the content (a view) automatically, with this code in the Public controller:
    <?php
    
    class Controller_Public extends Controller_Template &#123;
     
     public $template = 'layout/index';
     
     public function before()
     &#123;
      parent::before();
      $this->template->content = View::factory(Request::active()->controller.'/'.Request::active()->action, $data);
     }
     
    }
    

    (the controller corresponds with the directory in de views directory and the action corresponds with the filename of the view) So I don't need to load the view manually in every action_ function. This works great, except for passing through the array $data with the data for the view (because the function before() sends the view before I add data to $data). So how can I send data to a view, using this "automatic view loading" code?
  • See the View class documentation. Your view object is stored in $this->template->content, so use $this->template->content->set() to set variables.

Howdy, Stranger!

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

In this Discussion