2012-04-15 134 views
1

我定製我的笨路,這裏是我的routes.php文件的樣本:笨路由問題

$route['default_controller'] = 'maincontroller/main';                                              
$route['404_override'] = '';  

$route['test'] = 'maincontroller/test'; 

這裏是maincontroller類:

class Maincontroller extends CI_Controller 
{     
    public function __construct() 
    { 
     parent::__construct(); 
     $this->load->library('session'); 
     $this->init(); 
    } 


    public function main()   
    {  
     echo "in main"; 
    } 

    public function test() 
    { 
     echo "test"; 
    } 
} 

但是當我訪問此網址在我的瀏覽器中:/ test,我得到服務器的404頁面未找到錯誤。

我不知道我做錯了什麼,我遵循codeigniter用戶指南中的路線指南。

+0

哇,這工作....感謝。那麼如何設置路由,以便它始終在url中包含index.php,就像默認情況下那樣。 – dave 2012-04-15 21:15:01

回答

5

而不是去/測試,嘗試將/index.php/test

默認情況下笨貫穿的index.php一切。

您可以通過將.htaccess文件添加到站點根文件夾來更改此行爲。

這是直接從CodeIgniter用戶指南中取得的。

https://ellislab.com/codeigniter/user-guide/general/urls.html

的htaccess文件

RewriteEngine on 
RewriteCond $1 !^(index\.php|images|robots\.txt) 
RewriteRule ^(.*)$ /index.php/$1 [L] 

如果你這樣做,你還需要改變你的config/config.php文件

找到

$config['index_page'] = 'index.php'; 

它改變

$config['index_page'] = ''; 

當您想要在頁面(視圖)中創建鏈接時,您可以使用內置的site_url()函數,您可能需要爲此加載url助手。

例如

<?php echo site_url('test'); ?> 

這將考慮到$配置[「index_page」]設定,爲您的鏈接正確的網址。

+0

哇,工作....謝謝。那麼如何設置路由,以便它始終在url中包含index.php,就像默認情況下那樣。 – dave 2012-04-15 21:15:26

+0

接受。那麼我可以在哪裏可以使它默認爲index.php – dave 2012-04-15 21:17:58

+1

乾杯:)希望這個信息可以幫助,請檢查我添加的鏈接,以獲得有關CodeIgniter URL – Dale 2012-04-15 21:21:20

0

如果您嘗試從本地主機運行您的站點,請使用下面的代碼。 將.htaccess文件添加到站點根文件夾(例如:mysite)。

RewriteEngine on 
RewriteCond $1 !^(index\.php|images|robots\.txt) 
RewriteRule ^(.*)$ /root folder/index.php/$1 [L] 

託管在遠程主機服務器上的網站,使用此代碼

RewriteEngine on 
RewriteCond $1 !^(index\.php|images|robots\.txt) 
RewriteRule ^(.*)$ /index.php/$1 [L]