簡體   English   中英

簡單的 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

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

$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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM