0

I know this question has been asked in a similar fashion several times. However, I'm struggling to find any answers that would work in my situation. I primarily work on Microsoft projects and stepped in on this project to help during crunch.

Here's the situation.

We have a client who has a site with over 600 different pages. In reality each page uses the same template just populates with different data. We've developed a CMS for him which allows him to create new pages at will.

My ideal solution would allow me to store the name of a newly created page in a DB. Ex. new_page_1 has been created and now exists in the DB. Now when I type in www.mysite.com/new_page_1 this needs to go to a controller that looks up "new_page_1" (in DB) and if it exists loads a view (THIS VIEW WILL NEED TO BE USED FOR ALL 600 pages) which then takes other data from the DB and populates various sections.

So essentially, over 600+ pages need to use the same route array and map to the same controller which then maps to the same view.

I've tried using $route['(:any)'] = 'custom_controler/create/$1 and as well as the same array key but using main and _remap. No matter what every single time it tries to look for the page name in my views (which it will never exist because I'm using one generic view for 600 pages)

Any ideas on how to accomplish this?

UPDATE routes.php (this is the last line in the file)

$route['(:any)'] = "main/create/$1";

main.php (controller)

class Main extends MY_Controller {

public function __construct() {
    parent::__construct();
}
public function create($page)
{
    $c = new Category();
    $c->get_by_name(ucfirst($page));
    $this->load->view('site/index',$c);
}

}

the URL I'm attempting is sitename.servername.com/health sitename and servername obviously substituted.

The error I get is

An Error Was Encountered

Unable to load the requested file: health/main/create.php

Zach Johnson
  • 123
  • 9

2 Answers2

1

Is the error page you're seeing the CodeIgniter error template or a generic server error? That error string sounds very much like you are using Apache or Nginx (or whatever main webserver you use) and its actually not even resolving to your CodeIgniter app at all, but searching for a PHP file that doesn't exist. You'll probably need to use mod_rewrite or something like that to make that URL point at the CodeIgniter install.

Otherwise, your implementation doesn't look completely wrong: you probably need to make sure main.php is the default route as well.

Femi
  • 64,273
  • 8
  • 118
  • 148
  • Thats probably it, I believe we use Nginx. Like I said I'm not the backend web guy here so I'm not all too familiar with our webserver or even codeigniter for that matter. I'll have to discuss this with our lead web developer tomorrow and see if he can figure out why it's pointing to a file that doesn't exist. Thanks for your help!. – Zach Johnson May 23 '13 at 02:27
  • Actually there was a line of code in the My_Controller file that was forcing it to use the first segment of the uri as a class name, I pulled that out and it works fine now. – Zach Johnson May 23 '13 at 02:59
0

Your application will only look for the page name in your views if you tell it to. Your catch-all controller method should be checking the database for a valid page name, and then regardless of which pages validate, loads the same view (albeit with different data passed to the view).

Using an (:any) catch-all route is perfectly fine. Your controller code somewhere is what's throwing you off. Update your post with the code if you continue to struggle.

Aken Roberts
  • 13,012
  • 3
  • 34
  • 40