2013-03-21 33 views
3

我對CodeIgniter相當陌生,並且完成了我的第一個項目。但是,在我將其放在我的託管站點上之前,我想使用CodeIgniter在config文件夾中提供的routes.php文件清理URL。簡單的URL路由不能與CodeIgniter一起使用

我的網站會加載使用以下網址:

http://fakehost:8888/TheWorksPlumbing/index.php/theWorksPlumbingController/index/home 
http://fakehost:8888/TheWorksPlumbing/index.php/theWorksPlumbingController/index/about 
http://fakehost:8888/TheWorksPlumbing/index.php/theWorksPlumbingController/index/services 

,它也將使用默認的控制器的網址加載網頁:http://fakehost:8888/TheWorksPlumbing/

不過,我想對每一個url該網站的頁面,但不能得到它的工作。例如,我想有:

http://fakehost:8888/TheWorksPlumbing/home 
http://fakehost:8888/TheWorksPlumbing/about 
http://fakehost:8888/TheWorksPlumbing/services 

下面是theWorksPlumbingController控制文件中的代碼:

class TheWorksPlumbingController extends CI_Controller { 

    public function index($page = 'home'){ 

     if (!file_exists('application/views/'.$page.'.php')) { 
      show_404(); 
     } 

     $this->load->view('templates/header'); 
     $this->load->view($page); 
     $this->load->view('templates/footer'); 
    } 
} 

這裏是我的routes.php文件文件中的代碼不工作:

$route['default_controller'] = "theWorksPlumbingController"; 
$route['404_override'] = ''; 
$route['(:any)'] = "index.php/theWorksPlumbingController/index"; 

我需要添加或更改什麼才能讓網站加載/ home或/ about或/ services?

+0

快速完整性檢查,通過

http://www.example.com/index.php/theWorksPlumbingController/home http://www.example.com/index.php/theWorksPlumbingController/about http://www.example.com/index.php/theWorksPlumbingController/services 

而且路由可達什麼你的mod_rewrite規則是什麼樣子的? – David 2013-03-21 18:49:27

+0

大衛,我沒有觸及mod_rewrite規則。你的意思是.htaccess文件嗎?它說:拒絕所有在那裏..我認爲這是不好的? – ErinSKabbash 2013-03-21 18:53:13

+1

看着你回答的問題,它似乎不像你有.htaccess下的mod_rewrite規則。否則,我強烈建議你花一些時間閱讀Apache的用戶指南 - http://httpd.apache.org/docs/2.2/它們相當乾燥,但重要的是你對Apache可以爲你做什麼或可能做什麼有所瞭解。 – David 2013-03-21 21:11:17

回答

4
$route['home'] = 'theWorksPlumbingController/index/home'; 
$route['about'] = 'theWorksPlumbingController/index/about'; 
$route['services'] = 'theWorksPlumbingController/index/services'; 

可能會這樣做..雖然我從來沒有嘗試過這樣的設置。通常,在CI你犯了一個方法,爲每個頁面,像這樣:

class TheWorksPlumbingController extends CI_Controller { 

    public function home(){ 

     $this->load->view('templates/header'); 
     $this->load->view('home'); 
     $this->load->view('templates/footer'); 
    } 

    public function about(){ 

     $this->load->view('templates/header'); 
     $this->load->view('about'); 
     $this->load->view('templates/footer'); 
    } 

    public function services(){ 

     $this->load->view('templates/header'); 
     $this->load->view('services'); 
     $this->load->view('templates/footer'); 
    } 
} 

它通過

$route['home'] = 'theWorksPlumbingController/home'; 
$route['about'] = 'theWorksPlumbingController/about'; 
$route['services'] = 'theWorksPlumbingController/services'; 

http://ellislab.com/codeigniter/user-guide/general/routing.html

+0

我試圖將您的路線添加到我的routes.php ..但它沒有奏效。然後我把它們保存在routes.php文件中,並試圖將你建議的函數添加到控制器文件theWorksPlumbingController.php中,但那也不起作用?我是否缺少需要自動加載的內容?或者可能是.htaccess文件中的東西? – ErinSKabbash 2013-03-21 18:58:02

+0

確保擺脫'$ route ['(:any)'] =「index.php/theWorksPlumbingController/index」;'路由。 '.htaccess'完全不應該影響路線; .htaccess用於隱藏index.php等形式的url。 – stormdrain 2013-03-21 19:00:58

+0

啊,好的。我添加了index.php到網址,它工作。所以,現在我有http:// localhost:8888/TheWorksPlumbing/index.php/home,但是如何隱藏index.php? – ErinSKabbash 2013-03-21 19:03:46