[eluser]philpalmieri[/eluser]
a while back we needed the ability to have some static pages, but to give them access to the CI models...
This allowes a couple people in the office, and our clients to add files and hierarchy to the /views folder, and have it reflected on the domain with the proper URL, also keeping the .htaccess re-writes in place.
First step setup your .htaccess if it isnt already:
We are ignoring index.php, robots and a folder called global that we keep scripts, css etc in)
Code:
/*.htaccess */
RewriteEngine on
RewriteCond $1 !^(index\.php|robots\.txt|global)
RewriteRule ^(.*)$ /index.php/$1 [L]
Next Setup a controller called pages.php
This is where the fun is, load the models/helpers you want your static pages to have access too
Code:
/* controllers/pages.php */
<?php
class Pages extends Controller {
function Pages() {
parent::Controller();
$this->load->model("story", "story", TRUE);
$this->load->model("news_list", "nlist", TRUE);
$this->load->model("photo_list", "plist", TRUE);
$this->load->helper("text");
$this->load->helper('typography');
}
function index(){
$newurl = substr($this->uri->uri_string(), 1);
$newurl = str_replace(".html", "", $newurl);
if(strlen($newurl) == 0){
$this->load->view("index");
}elseif(file_exists(APPPATH."views/{$newurl}.php")){
$this->load->view($newurl);
}else{
echo $newurl;
show_404();
}
}
}
Now, to bring it all together, we need to modify the config/routes.php file so that the system will look for a controller first, then fall back on a static page, then to an error..
Code:
/* config/routes.php */
$route['default_controller'] = "pages";
$route['scaffolding_trigger'] = "";
$handle = opendir(APPPATH."controllers");
while (false !== ($file = readdir($handle))) {
if(substr($file, -4) == ".php"){
$route[substr($file, 0, -4)."(.*)"] = substr($file, 0, -4)."$1";
}
}
//print_r($route);
$route[':any'] = "pages/index";
finally, create a folder in your views folder called /views/test/. in there put an index.php, a foo.php etc. and you will be able to access them by going to site.com/test or .com/test/foo.html (we re-write the .html to php so it looks like static content
This is our static page, that our designers now have access to loop through dynamic data like recent blog posts, new products etc.
Code:
/* static page - this would be full HTML, but i cropped just a dynamic section for the post */
<div class="yui-g">
<div id="mn_bd">
<h1>Featured Installation Photos</h1>
<p>Below are several photos </p>
<!--BEGIN PHOTO BLOCK-->
<div id="photo_content">
<? foreach($this->plist->photos as $photo): ?>
<a href="/global/images/arizona/gallery/photos/<?=$photo->file_name?>" title="Arizona Installation" rel="Arizona Slideshow"><img src="/global/images/arizona/gallery/thumbs/<?=$photo->file_name?>" alt="<?=$photo->description?>" title="<?=$photo->title?>" width="186" height="120" /></a>
<? endforeach; ?>
</div>
<!--END PHOTO BLOCK-->
</div><!--END mn_bd CONTENT-->
</div><!--END MAIN BODY CONTENT-->
Well you get the idea
Phil