2012-03-12 41 views
0

我用CodIgniter開始一個PHP項目,我有一個項目在wamp/www/myproject下。當我開始本地主機,然後單擊我的項目URL看起來是這樣的:這是在CodeIgniter中的URL路由

localhost/myproject 

我想有這樣的路由,當有人來鍵入

localhost/myproject/box/123 

他應該被重定向到home控制器box方法。

我該怎麼辦?

+0

http://stackoverflow.com/questions/2584571/getting-started-with-url-routing-with-php-codeigniter – 2012-03-12 10:53:17

+0

@Eddy弗雷迪以及我試過,但它不工作 – 2012-03-12 10:55:02

+0

已經有一個很好的在文檔中的例子。 – itachi 2012-03-12 10:55:31

回答

3

轉到路由配置文件(我想你知道在哪裏),並補充一點:

$route['box/(:num)'] = 'home/box/$1'; 

任何有box/[any number]將被路由到home/box方法,以及比賽進行到(:num)作爲第一個參數。

所以你

http://localhost/myproject/index.php/box/123 

將被路由到:( 「因爲如果你使用的顯示」)

http://localhost/myproject/index.php/home/box/123 

(這我假設你沒有重寫引擎從URL中移除index.php尚)


From the guide

刪除index.php文件

默認情況下,index.php文件將被包含在你的URL

http://example.com/index.php/news/article/my_article 

您可以輕鬆地刪除此文件使用帶有一些簡單規則的.htaccess文件。下面是這樣一個文件的一個例子,使用「負」的方法中,一切都被重定向除指定的項目:

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

在上述例子中,比那些的index.php,圖像之外的任何HTTP請求時,而robots.txt被視爲您的index.php文件的請求。

+0

我試過你的建議解決方案,但是當我在url中輸入http:// localhost/mediabox/box/1時,它表示在服務器 – 2012-03-12 10:57:49

+0

上找不到所請求的url,你是使用apache重寫刪除'index.php /'或還沒有?因爲如果沒有,你的網站應該像'http:// localhost/mediabox/index/box/1' – Joseph 2012-03-12 10:59:37

+0

不,我不使用它...你能指導我該怎麼做嗎? – 2012-03-12 11:01:09