Base Controller for jQuery Slider

I'm at it again. 4th Fuel project and this time I want a jQuery Slider on Evvverryyyy page.. However, the data is coming from a database so I've added my class to the MY_controller.php file as I would do in codeigniter to create a base controller. This works fine on pages with their own controller that use this method to render the page:

$page_init = array('render_mode' => 'cms');
$this->fuel->pages->render('home', $this->vars, $page_init);

the problem is.. the pages that come directly out of the CMS and don't have their own controller and crash b/c the render method isn't being passed in $this->vars...

/* The MX_Controller class is autoloaded as required */
class MY_Controller extends CI_Controller {

public $vars;

function __construct()
{
parent::__construct();

$this->load->model('Slider_model');

$this->vars['homepage_sliders'] = $this->Slider_model->GetSliders();
}
}

Comments

  • edited 3:35AM
    You could try adding it to a variables file. The global variables file would load it for every request:
    http://docs.getfuelcms.com/general/pages-variables

    Also, something to consider, instead of creating a model for your slider, you could create a page layout that uses a template field that's repeatable. This allows you to have a set of fields (images, image alt, etc) that can be reordered around. We do this quite a bit for pages that have an image slider.
    http://docs.getfuelcms.com/general/forms#template
  • edited 3:35AM
    Okay so I tried adding a global variable and now the errors go away for the pages that aren't calling the base controller. However, it flat out doesn't work on the ones that do extend the base controller. This is a big issue to me because I like having data come out of the base controller. I don't want to change this.


    <?php (defined('BASEPATH')) OR exit('No direct script access allowed');

    /* The MX_Controller class is autoloaded as required */
    class MY_Controller extends CI_Controller {

    //public $vars;

    function __construct()
    {
    parent::__construct();

    $this->load->model('Slider_model');

    //$this->vars['homepage_sliders'] = $this->Slider_model->GetSliders();
    $vars['homepage_sliders'] = $this->Slider_model->GetSliders();
    }
    }




    <?php
    class Home extends MY_Controller {

    function __construct()
    {
    parent::__construct();
    }

    public function index()
    {
    $page_init = array('render_mode' => 'cms');
    $this->fuel->pages->render('home', $vars, $page_init);
    }

    }
    /* end of class */

    It can't find $vars... How do I make this work? .. and how do I make it work on the CMS without controllers?
  • edited 3:35AM
    I'm looking for away to add that variable globally..

    $this->fuel->pages->variables(['$key'=NULL]);

    ????
  • edited 3:35AM
    Ultimately, in the future, I need to build a shopping cart module for Fuel CMS and It's absolutely important that I understand how to pass back information from the base controller. Like cart total... and that needs to be on every single page and it has to come from a module.
  • edited 3:35AM
    If I'm understanding the controller correctly, it looks like you are setting a local $vars variable in the __construct method so the index method would never see it. If you assign the variable to $this->vars in the constructor and then pass $this->vars instead of $vars in your index method, that should work.
  • edited 3:35AM
    Yes, that's exactly what I was able to get to work .. the problem is... it doesn't work on pages pulled out of the CMS.. So, my big question is.. how do I add data to all pages b/c this page is going to have a jQuery slider AND I'm using a temperature API to post the current local temperature on every page.
  • edited 3:35AM
    You can also try using:
    $vars = $this->fuel->pagevars->retrieve($location);
    This will retrieve any variables assigned in a variables file or in the CMS.
  • edited 3:35AM
    Yeah... but that still doesn't give me the direction I'm looking for.

    BaseController->HomeController-> (WORKS)

    ->Golf Page (CMS Page / No Controller / Does not work)

    How do I get the base controller to inject $vars data into a CMS page that does not have a controller, basically a CMS page....
  • edited 3:35AM
    Think about the problem like this. I have a PHP cUrl script that grabs json data from a weather API. I need the temperature to show up on every single page.. How do I make that happen?
  • edited 3:35AM
    In short, you can't really do that. Pages not rendered through a normal controller get routed to the fuel/modules/fuel/controllers/page_router.php controller. So you won't be able to set controller variables on one controller (your BaseController) and have them appear in another controller (Page_router). Variables set in the CMS or in a _variables file can be be shared though. You may also look into inserting some of that logic into the layout file itself (perhaps extracting it out into a class or function).
  • edited 3:35AM
    Also, if you create a layout as a class, you can assign a "pre_process" hook that can be used to process the variables before delivering it to the view:
    http://docs.getfuelcms.com/general/layouts
  • edited 3:35AM
    Okay, I like the idea of the hook and is it possible to load data via the variables page?

    That's too bad I can't use the base controller to do this... This should be implemented in the future so I won't have to do these hacks..

    I would like to make a shopping cart for Fuel.
  • edited 3:35AM
    When you say the "variables page" do you mean the global.php variables file. If so, you can always use the global $CI object (e.g. get_instance()); and then load the model from that:
    $CI->load->model('slider_model'); $vars['sliders'] = $CI->slider_model-> GetSliders();
  • edited 3:35AM
    Nailed it.. Well, this is awesome for both of us. I need to make a blog or something and go all into tutorials about Fuel CMS and other development.. etc.. I have a lot of Fuel knowledge now.
Sign In or Register to comment.