本文介绍了Codeigniter - CMS的最佳路由配置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Codeigniter中创建一个自定义CMS,我需要一个机制来将一般页面路由到默认控制器,例如:

I would like to create a custom CMS within Codeigniter, and I need a mechanism to route general pages to a default controller - for instance:

mydomain.com/about
mydomain.com/services/maintenance

这些将通过我的页面控制器。 Codeigniter中的默认路由行为当然是路由到匹配的控制器和方法,所以在上面的例子中,它需要一个关于控制器和一个服务控制器。这显然不是一个实用或甚至明智的方法。

These would be routed through my pagehandler controller. The default routing behaviour in Codeigniter is of course to route to a matching controller and method, so with the above examples it would require an About controller and a Services controller. This is obviously not a practical or even sensible approach.

我看到下面的解决方案放置在routes.php:

I've seen the following solution to place in routes.php:

$route['^(?!admin|products).*'] = "pagehandler/$0";

但这会造成我自己的问题。例如,它只是在请求uri中查找products,如果找到到Products控制器的路由 - 但是如果我们有服务/产品作为CMS页面呢?这不会被路由到产品控制器?

But this poses it's own problems I believe. For example, it simply looks for "products" in the request uri and if found routes to the Products controller - but what if we have services/products as a CMS page? Does this not then get routed to the products controller?

有没有一个完美的方法呢?我不希望有一个路由,其中​​所有CMS内容以控制器名称为前缀,但我还需要能够一般地覆盖其他控制器的路由。

Is there a perfect approach to this? I don't wish to have a routing where all CMS content is prefixed with the controller name, but I also need to be able to generically override the routing for other controllers.

推荐答案

如果你使用CodeIgniter 2.0(已经稳定到足以使用几个月),那么你可以使用:

If you use CodeIgniter 2.0 (which has been stable enough to use for months) then you can use:

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

这将发送任何不是控制器,方法或有效路由到您的页面控制器。

This will send anything that isn't a controller, method or valid route to your pages controller. Then you can use whatever PHP you like to either show the page or show a much nicer 404 page.

阅读我的指南,解释如何。此外,您可能有兴趣使用现有的CMS,例如,现在接近最终的v1.0,并且有大量追随。

Read me guide explaining how you upgrade to CodeIgniter 2.0. Also, you might be interested in using an existing CMS such as PyroCMS which is now nearing the final v1.0 and has a massive following.

这篇关于Codeigniter - CMS的最佳路由配置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 00:39