5

我对 CodeIgniter 还很陌生,并且完成了我的第一个项目。然而,在我把它放到我的托管站点之前,我想使用 CodeIgniter 在配置文件夹中提供的 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

下面是 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?

4

1 回答 1

7
$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

于 2013-03-21T18:47:23.920 回答