Blog Archive

Wednesday 2 May 2012

Make CMS content appear in a controller view with FUEL CMS

Fuel CMS is a CMS based on the Codeigniter MVC framework. One of the first things I tried when setting it up was to create a controller and view that would display custom content, but would also show any CMS page data. That way, a client can add their copy to a view that would otherwise just loop over module data. They might, for example, have a document library view page (where data is sourced from a documents table) but want to add their own CMS advice to the page too.

It is possible to do this, but it needs a few steps that are not explicit from tutorials I have read.

This tutorial is based on v0.93 of the Fuel CMS and it may become redundant in later versions. Update v1.0 now makes the rendering method a bit simpler - see the end of this post.

Step one

Having installed Fuel CMS (see the user guide) you will need to fire up your editor of choice and create a controller. For this tutorial we’ll make a home controller, which of course will reside in /application/controllers/home.php.

   1:  <?php 
   2:  if (!defined('BASEPATH')) exit('No direct script access allowed');
   3:  class Home extends CI_Controller
   4:  { 
   5:      function __construct()
   6:      {
   7:          parent::__construct();
   8:      }
   9:      function home() 
  10:      {        
  11:          parent::Controller();    
  12:      }
  13:      function index()
  14:      {
  15:          $data = array();
  16:          $data['my_custom_content'] = 'Lorem ipsum dolor sit amet.';
  17:          $data['view'] = 'home/home'; 
  18:          $page_init = array('location' => 'home');        
  19:          $this->load->module_library(FUEL_FOLDER, 'fuel_page', $page_init);
  20:          $this->fuel_page->add_variables($data);
  21:          $this->fuel_page->render();    
  22:      }
  23:  }
  24:  ?>


Anyone familiar with Codeigniter will recognise what’s going on in this code. And there are few surprises from the Fuel CMS point of view either. We assign a string to the key value "my_custom_content", but this could be content from a model class or whatever you like. This is the content we want to see in the view, in addition to the CMS page data.

Note the definition of the $data[‘view’] and $page_init.

Step two

The view file can be as simple as this:

   1:  <?php fuel_set_var('layout', '')?>
   2:  <h1><?php echo fuel_var('page_title', 'Home page'); ?></h1>
   3:  <?php echo fuel_var('body', 'No CMS content'); ?> 
   4:  <h2>Controller content</h2>
   5:  <?php echo $my_custom_content; ?>


Here in line 1 we are erasing the layout  fuel_render() would ordinarily apply. Without this Fuel would grab the layout file located in application/views/_layouts/main.php. Why remove that functionality? Partly to demonstrate it’s possible, and partly because home page layouts are usually one-off designs not shared with the rest of a site.

In lines 2 and 3 we add the usual Fuel methods for adding CMS content with fuel_var(). Note that fuel_var() allows a second argument - this is the default value to show if the first argument is empty. After these, we add the content supplied by the controller. As usual with Codeigniter’s MVC framework, we access the key of the array passed in the controller ($my_custom_content), in this instance to the add_variables() method.

Step three

Add a CMS page with a location of “home” in the Fuel admin. Add some body content and a title to satisfy the variables we put in the view file (“page_title” & “body”).

Step four

You would be disappointed if you tried to access the url /home in your install at this point! We have one last configuration to make to get the home page to look as intended.

Open up the file MY_fuel.php in the application/config folder. Edit the “fuel_mode” value, and set it to "auto" if it is set to anything else:

   1:  $config['fuel_mode'] = 'auto';

This makes Fuel search the CMS for content matching the url as well as fetching a view.

Step five

There is a further change to make to ensure the view can respect the "published" status of CMS content (that is to say, when the page is un-published, the CMS content will not show). To effect this we need to edit the pagevariables_model.php file in the Fuel module itself (/fuel/modules/fuel/models/pagevariables_model.php). The _common_query()  method does not anticipate being called outside of the admin environment, so we need to ensure it will only show published pages when instanced outside of the admin. Replace the entire method with the following:

   1:      function _common_query()
   2:      {
   3:          $this->db->join($this->_tables['pages'], 
$this->_tables['pages'].'.id = '.$this->_tables['pagevars'].'.page_id', 'left');
   4:          // respect published status outside of admin
   5:          if (!defined('FUEL_ADMIN'))
   6:          {
   7:              $this->db->where(array($this->_tables['pages'].'.published' => 'yes'));
   8:          }
   9:      }

This fix might well be included in a future update of Fuel.

Step six

If you want to add a further nicety, you can edit the routes.php (also in the config folder) and change the line

   1:  $route['default_controller'] = 'fuel/page_router';

to

   1:  $route['default_controller'] = 'home';

Doing this will allow the home controller to serve / and /home.

Update (Sept 2012)

In v1.0 of Fuel CMS the render method has changed somewhat, making the process easier. There is now a pages object.

All that is now required is to set the url part to retrieve CMS content, and the view path like so:

$vars = $this->fuel->pagevars->retrieve($this->uri->uri_string());
$this->fuel->pages->render('home/index', $vars); 

1 comment:

  1. Thanks for posting this, I've been trying to figure out how to do this for a a few hours now and couldn't figure out what I was doing wrong.

    ReplyDelete

My top artists