gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 13:55:00 26 4
gpt4 key购买 nike

我是 CodeIgniter 的新手,正在完成我的第一个项目。但是,在我将它放在我的托管站点之前,我想使用 CodeIgniter 在配置文件夹中提供的 routes.php 文件清理 URL。

我的网站将使用以下 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

它还将使用默认 Controller url 加载主页:http://fakehost:8888/TheWorksPlumbing/

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

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

以下是 theWorksPlumbingController Controller 文件的代码:

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

关于php - 简单 URL 路由不适用于 CodeIgniter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15555494/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com