CodeIgniter Forums

Full Version: Serve a static page using a single controller w/o making its own controller
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have the following folder structure wherein inside the "views" folder I have several static pages organized by their "parent":


I have the following controller to serve all the views that are direct children of "pages".

class Pages extends CI_Controller
   function __construct()

   public function view($page = 'home')

       $data['title'] = strtoupper(str_replace(["-", "–"], ' ', $page));
       $this->load->view('templates/header', $data);
       $this->load->view('pages/'.$page, $data);
       $this->load->view('templates/footer', $data);

I have no problems linking to the top-level views under "pages" but when I traverse to a subfolder, it returns a 404.

htaccess for reference:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /iacuc/

# Removes index.php from ExpressionEngine URLs
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteRule (.*?)index\.php/*(.*) /iacuc/$1$2 [R=301,NE,L]

# Directs all EE web requests through the site index file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /iacuc/index.php/$1 [L]


PHP Code:
$controller_views 'pages/view';
$route['default_controller'] = $controller_views;
$route['404_override'] = '';
$route['translate_uri_dashes'] = TRUE;

$route['pages/(:any)'] = $controller_views.'/$1';
$route['(:any)'] = $controller_views.'/$1'

What I want to achieve: 

From the folder structure above, I want to show "organization.php" and "history.php" through Pages::view() and calling them in my header as:

<a href='<?php echo base_url().'about/organization'; ?>'>Company Organzation</a>

Current workaround:

I made specific classes similar to Pages::view() for each of these subfolder views such as About::organization() and About::history().

I have no idea if I can serve subfolder static pages without resorting to the strict "controller/method" pattern but I hope you can help me with this. 

Currently there are no specific questions like mine online so I am looking forward to your advise.
Your default controller cannot include a sub-directory

So what you need is a single startup page before going to your pages controller, like Welcome or Home.
(09-05-2016, 04:28 AM)InsiteFX Wrote: [ -> ]Your default controller cannot include a sub-directory

So what you need is a single startup page before going to your pages controller, like Welcome or Home.

Apologies, I do not quite understand how this works. 

When viewing my project, it should first direct to a "single startup page"; from here all raw links will be directed to my static page service controller.

What difference will this make? Or do you have a sample code on how to achieve this?
Most site today have what's called a landing page this is the first page on the web site from there just redirect to your page startup either through a button or continue link.
I am thankful for you noticing my problem @InsiteFX but as far as I know, the landing page is still served through my Pages::view() wherein the default parameter, as you can see in the Pages.php in my original post pertains to the name of my home page.

I just want to know if serving deep (at least a subfolder) view files within the views folder is possible without creating separate controller/method URIs for them just to abide by what I know CodeIgniter does.

If anyone knows how to deal with this I am looking forward to your help. I am still 100% unsure if what I want is possible because all over the Internet the tutorials and questions so far do not dig deeper into serving static pages that are at most one subfolder deep from the views folder.
Read this Reserved Routes.

CodeIgniter Users Guide - URI Routing