• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Serve a static page using a single controller w/o making its own controller

#1
I have the following folder structure wherein inside the "views" folder I have several static pages organized by their "parent":

Code:
my_project/
   application/
       views/
           pages/
               about.php
               about/
                   organization.php
                   history.php

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

Code:
<?php
class Pages extends CI_Controller
{
   function __construct()
   {
       parent::__construct();
   }

   public function view($page = 'home')
   {
       if(!file_exists(APPPATH.'views/pages/'.$page.'.php'))
       {
           show_404();
       }

       $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:

Code:
<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]
</IfModule>

route.php

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:

Code:
<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.
Reply

#2
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.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

#3
(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?
Reply

#4
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.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

#5
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.
Reply

#6
Read this Reserved Routes.

CodeIgniter Users Guide - URI Routing
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.