简单URL路由无法使用CodeIgniter


Simple URL Routing not working with CodeIgniter

我对CodeIgniter相当陌生,正在完成我的第一个项目。然而,在我把它放在我的托管网站上之前,我想使用CodeIgniter在config文件夹中提供的routes.php文件来清理URL。

我的网站将使用以下网址加载:

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

它还将使用默认的控制器url加载主页:http://fakehost:8888/TheWorksPlumbing/

然而,我想为网站的每个页面都有一个url,但无法使其工作。例如,我想要:

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

以下是WorksPlumbingController控制器文件的代码:

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?

$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');
    }
}

可通过到达

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

可通过路由

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

https://www.codeigniter.com/user_guide/general/routing.html