2012-11-08 61 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

相關問題