【问题标题】:How to delete the name of the controller in the URL with CodeIgniter?如何使用 CodeIgniter 删除 URL 中的控制器名称?
【发布时间】:2012-11-08 21:52:44
【问题描述】:

我的 url 中都包含 /main/,因为它是我的主控制器。 我想摆脱它。

我在用户指南中了解到,路由类只会重新路由我的 URL,而不是隐藏其中的一部分(根据他们的文档,它是第 1 段)。 因此,我发现了这个:

$route['(:any)'] = "auth/$1";

但是我有404错误(这是我对routing.php所做的唯一修改)

我相信我必须在 .htaccess 中执行此操作,但我不知道应该使用什么语法。目前,我的 .htaccess 是这个:

<IfModule mod_rewrite.c>
    SetEnv MAGIC_QUOTES 0
    SetEnv PHP_VER 5
# For security reasons, Option followsymlinks cannot be overridden.
#   Options +FollowSymlinks -MultiViews
    Options +SymLinksIfOwnerMatch -MultiViews
    RewriteEngine On
    DirectoryIndex index.php

    #Removes access to the system folder by users.
    #Additionally this will allow you to create a System.php controller,
    #previously this would not have been possible.
    #'system' can be replaced if you have renamed your system folder.
    RewriteCond %{REQUEST_URI} ^system.*
    RewriteRule ^(.*)$ /index.php?/$1 [L]

    #When your application folder isn't in the system folder
    #This snippet prevents user access to the application folder
    #Submitted by: Fabdrol
    #Rename 'application' to your applications folder name.
    RewriteCond %{REQUEST_URI} ^application.*
    RewriteRule ^(.*)$ /index.php?/$1 [L]

    #Checks to see if the user is attempting to access a valid file,
    #such as an image or css document, if this isn't true it sends the
    #request to index.php
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond $1 !^(index\.php|images|robots\.txt|css)
    RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>

<IfModule !mod_rewrite.c>
    # If we don't have mod_rewrite installed, all 404's
    # can be sent to index.php, and everything works as normal.
    # Submitted by: ElliotHaughin

    ErrorDocument 404 /index.php
</IfModule>

我正在使用几个控制器,我的链接类似于这个:

谢谢

【问题讨论】:

  • 如果您不同时发布 htaccess 和 routes.php 并准确告诉我们您想要什么,那将无法提供帮助。路由首先通过 htaccess,然后通过 routes.php 设置,因此您需要先检查 htaccess。通常你只需要让 htaccess 从你的 uri 中删除 index.php。同样在您的示例中,您将所有控制器调用路由到您的身份验证控制器,但您还说您希望它到主控制器?
  • 我在 htaccess 中看不到任何会导致意外 404 的内容。因此,您应该能够使用 routes.php 实现您想要的。你能在更具体一点之后解释一下你是什么以及你是如何构建你的 uri 和控制器的吗?根据我的经验,您使用 CI 的次数越多,您需要的路由就越少,因为您可以使用有意的结构来构建您的控制器。 (URI:Controller/Function/Parameter(s))那么您只需要让您的默认控制器(在您的情况下为 main)具有索引功能显示站点主页)
  • 谢谢,实际上我想从那种 url : website.com/Controller/Function/Parameter(s) 到 website.com/Function/Parameter(s) 至少对于我的主要控制器(对于另一个,例如博客,管理员......没关系,我想保留它)。所以如果我不应该在 .htaccess 文件中做任何事情,我应该在我的 routes.php 中做什么?我试过 "$route['(:any)'] = "auth/$1";"但是这样做时,单击我的链接时出现 404 错误..

标签: codeigniter url-rewriting routing


【解决方案1】:

试试这个:

 $route['blog'] = 'blog';  // seem stupid but otherwise the (:any) route below will send it to main
  $route['blog/(:any)'] = 'blog/$1'; // to have blog cotroller work
  $route['blog/(:any)/(:any)'] = 'blog/$1/$2';
  // repeat for other controllers

 $route['(:any)'] = "main/$1";
 $route['(:any)/(:any)'] = "main/$1/$2"; // if you use other uri parameter
 $route['default_controller'] = 'main'; // to have your main page work...

并将所有对其他控制器的调用放在此指令之前,因为这将阻止所有其他控制器调用。

【讨论】:

  • 谢谢!所以如果我有 1000 个控制器,我应该做 1000 次吗?什么是我的主控制器(和其他一些控制器)有 3 个或更多参数?我应该每次都使用 /$1/$2/$3 ... 吗?我尝试使用和不使用该行访问 main/news/24 : $route['(:any)/(:any)'] = "main/$1/$2";我看不出有什么区别,这条线有必要吗?
  • 不,绝对不是(尽管我从没想过你会在一个应用程序中拥有 1000 个控制器)。另一种解决方案可能是硬编码主控制器功能以路由到路由或 htaccess 中的 main/(function),然后您不需要对其他控制器 uri 做任何事情!
  • @MilesM。假设您的默认控制器仅包含索引功能,并且所有其他页面都有其他控制器及其相应的 URI 名称,那么您不需要任何路由,除了默认值。但是现在你用 main 构建了你的应用程序做了很多事情......
  • 嗯,我明白了,最好的做法是什么?所以现在,我只添加了 30 行,每个控制器 3 行,并从任何 href 中删除了 main/。它似乎工作正常。现在,假设我想再次重写 uri,通过标题更改新闻的 ID(我通过 url 传递给我的函数),这是同一个过程吗?因为这一次,ID 和标题都是动态生成的。可能离原来的话题太远了,我只是想知道在哪里看。无论如何,非常感谢您的帮助 CI Jedi !
  • @MilesM。我想最好的解决方案是将重写的新闻 uri 转发给控制器,让您的新闻模型为您找到相应的 id?要路由它,您需要对每篇文章进行硬编码(不是动态的!)
猜你喜欢
  • 2018-01-10
  • 1970-01-01
  • 2011-08-04
  • 1970-01-01
  • 2016-03-25
  • 2011-09-05
  • 2014-12-16
  • 2016-11-02
  • 1970-01-01
相关资源
最近更新 更多