[eluser]Lucas3677[/eluser]
Hi Randy,
Thanks for all the help you've given me so far. I've come up with a solution that works me (that you may disagree with), and I'll do some testing on it to make sure there are no bugs. Here is what I did:
application/config/autoload.php
Code:
//...
$autoload['model'] = array('cms_model');
//...
application/controllers/_cms.php
Code:
<?php
class _cms extends Controller
{
function __construct()
{
parent::Controller();
}
function load_page()
{
$page = implode('/', $this->uri->segment_array());
$query = $this->cms_model->getPage($page);
if ($query->num_rows() > 0)
{
$page = $query->row();
$data['content'] = $page->content;
$this->load->view('cms_template', $data);
}
else
{
show_404($page . ' [CMS]');
}
}
}
application/models/cms_model.php
Code:
<?php
class cms_model extends Model
{
function __construct()
{
parent::Model();
}
function getPage($path)
{
$this->db->where('uri', $path);
return $this->db->get('pages', 1);
}
}
application/views/cms_template.php
Code:
<h1>CMS Sample Page</h1>
<p><?=$content;?></p>
application/libraries/MY_Router.php
Code:
<?php
class MY_Router extends CI_Router
{
function __construct()
{
parent::CI_Router();
}
function _validate_request($segments)
{
// Does the requested controller exist in the root folder?
if (file_exists(APPPATH.'controllers/'.$segments[0].EXT))
{
if (isset($segments[1]) && $segments[1] != 'index')
{
$data = file_get_contents(APPPATH.'controllers/'.$segments[0].EXT);
if (!preg_match("/\bfunction\w+" . $segments[1] . "\(/i", $data))
{
return $this->cmsHook();
}
}
return $segments;
}
// Is the controller in a sub-folder?
if (is_dir(APPPATH.'controllers/'.$segments[0]))
{
// Set the directory and remove it from the segment array
$this->set_directory($segments[0]);
$segments = array_slice($segments, 1);
if (count($segments) > 0)
{
// Does the requested controller exist in the sub-folder?
if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$segments[0].EXT))
{
return $this->cmsHook();
//show_404($this->fetch_directory().$segments[0]);
}
}
else
{
$this->set_class($this->default_controller);
$this->set_method('index');
// Does the default controller exist in the sub-folder?
if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$this->default_controller.EXT))
{
$this->directory = '';
return array();
}
}
return $segments;
}
return $this->cmsHook();
// Can't find the requested controller...
//show_404($segments[0]);
}
function cmsHook()
{
$segments[0] = '_cms';
$segments[1] = 'load_page';
$this->set_class($segments[0]);
$this->set_method($segments[1]);
return $segments;
}
}
I have also solved the problem where the controller exists but the method doesn't (welcome/index vs. welcome/index2) by reading the controller file (if it exists) and using a simple regex match. This probably isn't the most efficient, but for now, it is fast enough to be good enough.
I know you don't like it when people hack the core files, but here is my reasoning behind it:
1. Using the pre_system hooks, I would have to essentially rewrite the whole controller loading process because it hasn't been loaded yet. This would be redundant (and not very pragmatic
).
2. I want to be able to use the templates in the /view directory for the CMS pages as well, so it would be most logical to have all those classes loaded too - might as well let the normal CI system load it up for me to prevent duplication and possible bugs.
What do you think?