2013-03-21 34 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