Codeigniter 中的 URI 路由

发布于 2024-09-02 01:03:10 字数 359 浏览 5 评论 0原文

我的 CI 网站运行良好,只是 URL 有点难看。 我应该采取什么方法来路由显示的内容:

http://domain.com /content/index/6/Planning

到网址:

http://domain.com/Planning

我很困惑是否应该在路由文件中或在我的 .htaccess 中完成此操作,

谢谢

I have my CI site working well except the URL's are a bit ugly.
What approach should I take to enable me to route what is displaying at:

http://domain.com/content/index/6/Planning

to the url:

http://domain.com/Planning

I'm confused about whether this should be be done in the routes file or in my .htaccess

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甜点 2024-09-09 01:03:11

http://codeigniter.com/user_guide/general/routing.html

你应该能够通过本页上的一些示例来实现这一点

http://codeigniter.com/user_guide/general/routing.html

you should be able to accomplish that with some of the examples on this page

来世叙缘 2024-09-09 01:03:10

有几种方法可以设置 config/routes.php,适合性取决于您的要求。

  1. 每个页面的路由,如果您只想路由几个页面:

    $route['规划'] = 'content/index/6';  
    $route['工作'] = '内容/索引/7';  
    // ETC。
    
  2. 您可以使用后备 url,它将在所有其他路由规则之后匹配 - 这意味着您必须设置在后备规则之前可能匹配此规则的规则。这也意味着你失去了ID,并且必须根据标题查询数据库:

    $route['register'] = 'register'; // 这将匹配后备规则  
    $route['([azA-Z1-9_]+)'] = '内容/索引/$1'; // 字母、数字和下划线  
    // 您将收到“Planning”作为 Content::index 方法的参数
    
  3. 或者你可以制定策略,所有内容的URL必须以大写字母开头,在这种情况下,你不必担心其他路由规则

    $route['([AZ]{1}[azA-Z1-9_]+)'] = '内容/索引/$1';  
    // 同样,您将收到“Planning”作为 Content::index 方法的参数
    
  4. 你仍然需要数字 ID,所以你不必更改控制器/模型:

    $route['(\d+)/[azA-Z1-9_]+'] = '内容/索引/$1';  
    // 路线现在看起来更难看:http://domain.com/6/Planning
    

There are couple of ways to set up config/routes.php, the suitability depends on your requirements.

  1. Route for each page, if you have just a couple of pages that you want to route:

    $route['Planning'] = 'content/index/6';  
    $route['Working'] = 'content/index/7';  
    // etc.
    
  2. You can use fallback url, that will match after all other route rules - that means you must set rules that might match this rule before the fallback rule. It also means you loose ID, and have to query database based on the title:

    $route['register'] = 'register'; // this would match the fallback rule  
    $route['([a-z-A-Z1-9_]+)'] = 'content/index/$1'; // letters, numbers and underscore  
    // you'll receive "Planning" as parameter to Content::index method
    
  3. Or you can have policy that all urls to content must start with capital letter, in that case you don't have to worry about other route rules

    $route['([A-Z]{1}[a-z-A-Z1-9_]+)'] = 'content/index/$1';  
    // again, you'll receive "Planning" as parameter to Content::index method
    
  4. You still want the numerical ID, so you don't have to change the controller/model:

    $route['(\d+)/[a-z-A-Z1-9_]+'] = 'content/index/$1';  
    // routes now look uglier: http://domain.com/6/Planning
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文