Question

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

Was it helpful?

Solution

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top