简单的URL路由不使用CodeIgniter

Eri*_*ash 5 php routing 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
Run Code Online (Sandbox Code Playgroud)

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

但是,我希望网站的每个页面都有一个网址,但无法让它工作.例如,我想:

http://fakehost:8888/TheWorksPlumbing/home
http://fakehost:8888/TheWorksPlumbing/about
http://fakehost:8888/TheWorksPlumbing/services
Run Code Online (Sandbox Code Playgroud)

以下是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');
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我的routes.php文件中的代码不起作用:

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

我需要添加或更改以使网站只加载/ home或/ about或/ services?

sto*_*ain 7

$route['home'] = 'theWorksPlumbingController/index/home';
$route['about'] = 'theWorksPlumbingController/index/about';
$route['services'] = 'theWorksPlumbingController/index/services'; 
Run Code Online (Sandbox Code Playgroud)

可能会这样做..虽然我从未尝试过这样的设置.通常在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');
    }
}
Run Code Online (Sandbox Code Playgroud)

可以通过

http://www.example.com/index.php/theWorksPlumbingController/home
http://www.example.com/index.php/theWorksPlumbingController/about
http://www.example.com/index.php/theWorksPlumbingController/services
Run Code Online (Sandbox Code Playgroud)

通过路由可路由

$route['home'] = 'theWorksPlumbingController/home';
$route['about'] = 'theWorksPlumbingController/about';
$route['services'] = 'theWorksPlumbingController/services';
Run Code Online (Sandbox Code Playgroud)

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