0

I’m a recent user of Codeigniter and am developing a simple backend CMS to manage pages.

Based on a URL (in this example I have hidden “index.php”) : mysite.com/pagename

I would like the system to detect if there is a value of “pagename” in my database, if there is, I need the system to re-route to a custom controller (eg: Pagemaker) and if there is no record called pagename, just do it’s normal thing (i.e. find a controller called pagename)

Currently I have:

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

whereby all requests are forwarded to my custom function.

However I want to change this structure so that if the page does NOT exist in the db, the traditional codeigniter request process is followed.

Can anyone offer any advice about how to complete this? Or any advice about routing custom CMS’s in codeigniter in general?

2 Answers 2

1

The best solution is to upgrade to CI 2.0 because it's stable enough and it gives you plenty of useful features.

In your case, set the following route:

$route['404_override'] = 'pagemaker';

If the router doesn't know where to go it just goes to pagemaker controller. This can then check if the first uri segment exists and if not you create a custom 404 page instead of the crappy default one.

And I don't want to hear any of this "Oh but it's not released yet" crap, I've been using it CI 2.0 for almost a year. ;-)

5
  • OK I'm going to upgrade... I'm scared. Hold me Phil ;-)
    – Mazatec
    Jan 14, 2011 at 14:03
  • Fine, but keep your hands to yourself. Jan 15, 2011 at 12:58
  • just a quick question: why is this route not mentioned in the user guide which ships with ci 2.0?
    – Mazatec
    Jan 17, 2011 at 15:04
  • If anyone comes across this in the future, the discussed route: $route['404_override'] must contain both a controller and a method i.e in my example : $route['404_override'] = 'pagemaker/create' it will not work otherwise
    – Mazatec
    Jan 19, 2011 at 10:23
  • This was fixed in CodeIgniter Reactor and will most likely be fixed in Core soon. Jan 19, 2011 at 12:06
0

I can think of two possibilities:

1) Edit your custom function to let it redirect your client when page's not in the db pseudo code:

if($dbresult == null){
    redirect("http://yoursite.com/"+$this->uri->segment(3));
}

2) Edit the router class of CI so it will first check if the page's in the db and if not, just continues. This may be somewhat messier as you need a db connection in your Router.php

1
  • the problem with this is that your system is constantly going in a loop - redirecting back to the same pagemaker class every time and wont work.
    – Mazatec
    Jan 14, 2011 at 13:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.