Code Igniter url routing question (using trailing slashes)

与世无争的帅哥 提交于 2021-02-10 14:40:14

问题


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?


回答1:


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/.



来源:https://stackoverflow.com/questions/1741505/code-igniter-url-routing-question-using-trailing-slashes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!