php - Codeigniter 中的 URI 路由

标签 php codeigniter routes uri

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

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

到网址:

http://domain.com/Planning

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

谢谢

最佳答案

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

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

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

    $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. 或者您可以制定策略,所有内容的 URL 都必须以大写字母开头,在这种情况下,您不必担心其他路由规则

    $route['([A-Z]{1}[a-z-A-Z1-9_]+)'] = 'content/index/$1';  
    // again, you'll receive "Planning" as parameter to Content::index method
    
  4. 您仍然需要数字 ID,因此不必更改 Controller /型号:

    $route['(\d+)/[a-z-A-Z1-9_]+'] = 'content/index/$1';  
    // routes now look uglier: http://domain.com/6/Planning
    

关于php - Codeigniter 中的 URI 路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2868126/

相关文章:

php - MYSQL|来自两个表的请求2

jquery - 可滚动表格不同宽度列

javascript - 加载资源失败 : The server responded with a status of 404 (not found)

ios - 具有 MVVM 架构的 Swiftui 在复杂应用程序中导航屏幕

ruby-on-rails - rails : Get path/url without knowing the object's class (sitemap for multiple models)

php - 从字符串中删除值

php - 使用 if 显示表中的记录 - PHP

javascript - 在同一页面上的 php 中获取 JavaScript 变量

共享 SSL 中的 Codeigniter

javascript - Next.js 将静态路由与动态路由重叠