php 简单的 URL 路由不适用于 CodeIgniter

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15555494/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 09:28:05  来源:igfitidea点击:

Simple URL Routing not working with CodeIgniter

phpcodeigniterrouting

提问by ErinSKabbash

I am fairly new to CodeIgniter and finishing up my first project. However, before I put it up on my hosting site I would like to clean up the URL's using the routes.php file that CodeIgniter provides in the config folder.

我对 CodeIgniter 还很陌生,并完成了我的第一个项目。但是,在我将它放在我的托管站点之前,我想使用 CodeIgniter 在 config 文件夹中提供的 routes.php 文件清理 URL。

My site will load using the following urls:

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

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

and it will also load the homepage by using the default controller url of: http://fakehost:8888/TheWorksPlumbing/

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

However, I want to have a url for each page of the site but cannot get it to work. For example I would like to have:

但是,我想为网站的每个页面设置一个 url,但无法使其正常工作。例如,我想要:

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

Here is the code for the theWorksPlumbingController Controller file:

下面是 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');
    }
}

Here is the code in my routes.php file that is not working:

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

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

What do I need to add or change to get the site to just load /home or /about or /services?

我需要添加或更改什么才能使站点只加载 /home 或 /about 或 /services?

回答by stormdrain

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

Might do it.. although I've never tried it with a setup like that. Typically in CI you make a method for each page like so:

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

which are reachable via

可以通过

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

And routable via

并且可路由通过

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

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

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