开发者

How to deal with routing when developing a custom CMS in Codeigniter

I’m a recent user of Codeign开发者_运维百科iter 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?


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. ;-)


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜