代码点火器网址路由问题(使用斜杠) [英] Code Igniter url routing question (using trailing slashes)

查看:55
本文介绍了代码点火器网址路由问题(使用斜杠)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是codeIgniter的新手.

我对自己开发的网站上的网址非常特别.是否可以创建这些类型的网址?通常,我开发的网站具有集成的管理界面,并在斜杠后在网址末尾添加了新的,编辑或删除的内容.

以下是一些假设的示例(其中一个带有管理员网址):

顶级页面(不带斜杠)

  site.com/aboutsite.com/contactsite.com/contact/edit 

部分索引列表(列表带有斜杠)

  site.com/blog/site.com/products/site.com/products/edit 

版面页面(列表中带有斜杠)

  site.com/blog/first-postsite.com/products/最佳产品site.com/products/newsite.com/products/best-product/delete 

板块类别

  site.com/blog/code-questions/site.com/products/red-products/site.com/products/red-products/delete 

我看到的第一个问题是将带有尾部斜杠的URL发送到另一个控制器,然后又没有其他URL.由于您无法将它们添加到路由文件中.例如,对于顶级页面,我怎么知道要调用Pages控制器?除了节索引列表,我该如何告诉他们?我不能在routes.php中添加尾部斜杠!

  site.com/aboutsite.com/blog/ 

与章节页面和类别相同.

通常,我过去使用.htaccess文件来完成此操作.

过去在我自己的应用程序中如何为网址构造htaccess文件的一些示例

  RewriteRule ^ new $ index.php?static& newRewriteRule ^ edit $ index.php?editRewriteRule ^([a-z0-9 \-] +)$ index.php?static& post = $ 1RewriteRule ^([a-z0-9 \-] +)/edit $ index.php?static& edit = $ 1RewriteRule ^([a-z0-9 \-] +)/$ index.php?section = $ 1RewriteRule ^([a-z0-9 \-] +)/new $ index.php?section = $ 1& newRewriteRule ^([a-z0-9 \-] +)/([a-z0-9 \-] +)$ index.php?section = $ 1& post = $ 2RewriteRule ^([a-z0-9 \-] +)/([a-z0-9 \-] +)/$ index.php?section = $ 1& category = $ 2 

无论如何,是否可以使用codeIgniter来做到这一点?我应该只在控制器生成的URL上重写规则吗?是否可以使用routing.php文件执行此操作?如果codeIgniter不这样做,您能建议一个可以的框架吗?

当绑定到控制器类名称时,如何在URL中使用连字符?

解决方案

CodeIgniter具有处理URI路由的相当不错的方法( http://codeigniter.com/user_guide/general/routing.html ).

默认情况下,如果您使用CodeIgniter自己的.htaccess( http://codeigniter.com/user_guide/general/urls.html ),将所有请求细分为漂亮的段.

您可以轻松地使用CodeIgniter的routing.php文件生成您想要的这些URL.请通过 http://codeigniter.com/user_guide/参阅其非常易于遵循的文档..>

I am brand new to codeIgniter.

I am very particular about urls on the sites that I develop. Is it possible to create these sorts of urls? Generally sites I develop have an integrated admin interface as well with new, edit or delete added onto the end of the url following a slash.

Here are some hypothetical examples (one with an admin url):

top level pages (no trailing slash)

site.com/about
site.com/contact
site.com/contact/edit

section index lists (lists have trailing slash)

site.com/blog/
site.com/products/
site.com/products/edit

section pages (lists have trailing slash)

site.com/blog/first-post
site.com/products/best-product
site.com/products/new
site.com/products/best-product/delete

section categories

site.com/blog/code-questions/
site.com/products/red-products/
site.com/products/red-products/delete

the first problem I see is sending a url with trailing slash to a different controller then without one. Since you can't add them in the routing file. For instance with top level pages how would I know to call the Pages controller? how do I tell them apart from section index lists? I can't add trailing slashes in routes.php!

site.com/about
site.com/blog/

same with the section pages vs categories.

generally I have done this in the past with an .htaccess file.

some examples of how I structure htaccess files for urls in the past in my own applications

RewriteRule ^new$ index.php?static&new
RewriteRule ^edit$ index.php?edit
RewriteRule ^([a-z0-9\-]+)$ index.php?static&post=$1
RewriteRule ^([a-z0-9\-]+)/edit$ index.php?static&edit=$1

RewriteRule ^([a-z0-9\-]+)/$ index.php?section=$1
RewriteRule ^([a-z0-9\-]+)/new$ index.php?section=$1&new
RewriteRule ^([a-z0-9\-]+)/([a-z0-9\-]+)$ index.php?section=$1&post=$2
RewriteRule ^([a-z0-9\-]+)/([a-z0-9\-]+)/$ index.php?section=$1&category=$2

Is there anyway to do this with codeIgniter? Should I just slather rewrite rules on top of the controller generated urls? Is it possible to do this with the routing.php file? If codeIgniter doesn't do this, could you suggest a framework that can?

Also How do I handle using hyphens in the url when tied to the controller class name?

解决方案

CodeIgniter has a pretty nice way of handling URI routing (http://codeigniter.com/user_guide/general/routing.html).

By default if you use CodeIgniter's own .htaccess (http://codeigniter.com/user_guide/general/urls.html), all requests made into pretty segments.

You could easily use CodeIgniter's routing.php file to generate these URL's you're after. Please refer to their very easy to follow documentation at http://codeigniter.com/user_guide/.

这篇关于代码点火器网址路由问题(使用斜杠)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆