如何设置动态路由在CodeIgniter使用slug? [英] How to set dynamic route to use slug in CodeIgniter?

查看:277
本文介绍了如何设置动态路由在CodeIgniter使用slug?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个名为

的控制器有一种方法

slug_on_the_fly

slug_on_the_fly($ slug)

我的路线怎么样?

例如对于博客控制器很容易:

E.g. for blog controller it would be easy:

$route['blog/(:any)'] = 'pages/slug_on_the_fly/$1';

,然后 http:// localhost / blog / name-of-the -article 工作不好

但是,如果我想像没有 blog 因此

However, what if I want to do it like without blog so e.g.

http:// localhost / name-of-the-article http:// localhost / another-article-blablabla

如何做, $ route ['friends'] ='users'; $ route ['about-us'] ='pages / about_us' code>?

How to do it and don't break another routes e.g. $route['friends'] = 'users'; or $route['about-us'] = 'pages/about_us'; ?

因为如果我这样做:
$ route ['(:any)'] ='pages / slug_on_the_fly / $ 1';

它可能会毁掉一切或?

推荐答案

网址按以下顺序路由:


  1. $
  1. Explicit routes in $route (routes.php) are checked in order.
  2. An implicit route [folder/]controller/methodname/args... is attempted as a fallback.

如果有少量已知显式路由,您只需将它们添加到 $ route

If have a small number of known explicit routes, you can just add them to $route:

$route['(my-slug|my-other-slug|my-third-slug)'] = 'pages/slug_on_the_fly/$1'

(路由键实际上作为正则表达式解析为:any :num 被重写为。+ [0-9] + 。)

(Routes keys are really parsed as regular expressions with :any and :num are rewritten to .+ and [0-9]+.)

如果您有大量此类路由(可能不是一个好主意,BTW!),您只需添加通配符路由到 $ route的 end

If you have a large number of such routes (probably not a good idea, BTW!) you can just add a wildcard route to the end of $route:

$route['([^/]+)/?'] = 'pages/slug_on_the_fly/$1'

正则表达式的意思是任何没有斜杠的网址。如果您有任何其他限制,您可以细化此描述您的slug格式。 (一个好的是 [a-z0-9 - ] + 。)如果你的控制器在db中找到slug,你就完成了。如果没有,它必须服务一个404。

The regex here means "any url that has no slashes (except maybe last)". You can refine this to describe your slug format if you have any other restrictions. (A good one is [a-z0-9-]+.) If your controller finds the slug in the db, you're done. If it doesn't, it must serve a 404.

然而,你放弃一些隐式路由的可能性,因为Codeigniter不提供任何方式为控制器放弃一条路由回路由器。例如,如果你有一个名为foo的控制器,你想要一个像 / foo 的路径到 Foo :: index() code>,您必须为这种情况添加一个显式路由,因为它将被此路由捕获并发送到 Pages :: slug_on_the_fly('foo')一般来说,你不应该有slugs也是控制器类名!这是为什么你应该有非常少的这些url-slugs,如果你有任何!

However, you give up the possibility of some implicit routing as Codeigniter doesn't provide any way for a controller to "give up" a route back to the router. For example, if you have a controller named 'foo' and you want a url like /foo to route to Foo::index(), you must add an explicit route for this case because it would be caught by this route and sent to Pages::slug_on_the_fly('foo') instead. In general, you should not have slugs which are also controller class names! This is why you should have a very small number of these url-slugs, if you have any at all!

如果您有两个大量的这些显式路由,您不愿意遵守这些隐式路由限制,您可以尝试将它们动态添加到 $ route

If you have both a large number of these explicit routes and you are not willing to abide by these implicit routing limitations, you can try adding them to $route dynamically:


  1. 创建 routes_extra.php 文件,其中 routes.php 包括在结尾。

  2. 子类 Router.php 并添加一个新的路由

  1. Make a routes_extra.php file which routes.php includes at the end. Write new routes to it as part of saving a page or when you build/deploy the site.
  2. Subclass Router.php and add a new routing layer.
  3. Add a pre_system hook which adds the routes.


  • 添加 pre_system

    我相信还有其他方法。

    I'm sure there are other ways.

    这篇关于如何设置动态路由在CodeIgniter使用slug?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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