2012-11-08 63 views
0

我有网址中的所有包含/ main /的url,因为它是我的主控制器。 我想摆脱它。如何使用CodeIgniter删除URL中控制器的名称?

我在用户指南中明白,路由类只会重新路由我的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> 

我使用几个控制器和我的链接是类似这样的:

感谢

+0

不能帮助,如果你不发布htaccess和routes.php,并确切地告诉我们你想要什么。路由首先通过htaccess然后通过routes.php设置,因此您需要首先检查htaccess。通常你只需要让htaccess从你的uri中删除index.php。同样在你的例子中,你将所有的控制器调用路由到你的认证控制器,但你也说你想让它到主控制器? – jtheman

+0

谢谢,请参阅编辑 –

+0

没有什么我在htaccess中可以看到会导致意外404.因此,你应该能够通过routes.php实现你想要的。你能解释一下你的具体情况,以及你是如何构建你的uri和控制器的吗?与CI一起工作的越多,您需要的路由就越少,这是我的经验,因为您可以使用有意识的结构来构建您的控制器。 (URI:控制器/函数/参数(s)),那么你只需要让你的默认控制器(主要在你的情况下)具有索引功能显示网站主页) – jtheman

回答

2

试试这个:

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

并把这个指令之前所有的呼叫到其他控制器,因为这将阻止所有其他控制器调用。

+0

谢谢! 所以如果我有1000个控制器,我应该做1000次?我的主控制器(和其他一些)有什么类似3个或更多的参数?我应该每次使用/ $ 1/$ 2/$ 3 ...吗? 我尝试访问主要/新闻/ 24有和没有该行:$ route ['(:any)/(:any)'] =“main/$ 1/$ 2”; 我看不出有什么区别,这一行是否必要? –

+0

绝对不是(即使我从来不认为你会在一个应用程序中有1000个控制器)。另一种解决方案可以是将主控制器功能硬编码到路由或htaccess中的main /(function),那么你不需要对其他控制器的uri进行任何操作! – jtheman

+0

@MilesM。假设您的默认控制器只包含索引函数,而其他所有页面都有其他控制器及其相应的URI名称,那么除了缺省值外,您不需要任何路由。但是,现在,你主要做了你的应用程序,主要做很多事情... – jtheman

相关问题