问题描述
我是 CodeIgniter 的新手,我刚刚阅读了他们的一些用户指南,以帮助我了解如何使用多个控制器.
I'm new to CodeIgniter and I've just gone through some of their user guide to help me understand how to do multiple controllers.
我已经想出了如何使用他们的示例之一加载多个页面.这是我的默认控制器,名为 Site.php:
I have figured out how to load multiple pages using one of their examples. This is my default controller, called Site.php:
class Site extends CI_Controller {
public function index($page = 'home') {
if ( ! file_exists(APPPATH.'/views/'.$page.'.php')) {
// Whoops, we don't have a page for that!
show_404();
}
$data['title'] = ucfirst($page);
$this->load->view('header', $data);
$this->load->view($page, $data);
$this->load->view('footer', $data);
}
}
这一切都在我的 routes.php 上运行良好:
This all works well and good with my routes.php:
$route['default_controller'] = 'site';
$route['(:any)'] = 'site/index/$1';
例如,当我转到 localhost/website/index.php/about 时,我有一些视图可以正确加载.
I have a few views that do load correctly when I go to localhost/website/index.php/about, for example.
现在,进一步了解控制器和 URI 路由.我创建了一个新的控制器,application/controllers/MyController.php,它的编码如下:
Now, to further learn about controllers and URI routing. I created a new controller, application/controllers/MyController.php and it is coded as such:
class MyController extends CI_Controller {
public function index() {
$this->load->view('header');
$this->load->view('my_controller');
$this->load->view('footer');
}
}
问题是,我不知道如何访问此页面.我知道默认的 URI 模式是 example.com/class/function/id/ 但我尝试了 localhost/website/mycontroller 但它不起作用.Apache 返回 404.我已经尝试了很多操作,甚至像输入 localhost/website/index.php/mycontroller 一样愚蠢,但当然这不起作用.
The thing is, I don't know how to access this page.
I understand that the default URI pattern is example.com/class/function/id/ but I have tried localhost/website/mycontroller and it doesn't work. Apache returns 404. I've tried many manipulations of this, even going as stupid as typing localhost/website/index.php/mycontroller but of course that doesn't work.
使用多个控制器的正确方法是什么?我在这里做错了什么?请帮助我学习,谢谢!
(另外,这是我在 StackOverflow 上的第一个正确问题,如果我做错了什么或格式不正确,请告诉我!)
推荐答案
问题在于这个规则:
$route['(:any)'] = 'site/index/$1';
拦截一切...并将其传递给Site控制器,作为index方法的参数.所以如果你打电话:
intercepts everything... and passes it to the Site controller, as an argument to the index method. So if you call:
/mycontroller
它实际上会映射到:
/site/index/mycontroller
路由发生在应用程序甚至查看控制器之前,并且路由规则会按照编写顺序进行考虑.
The routing happens before the app even looks at the controllers, and the routing rules are considered in order they are written.
因此,您需要将此规则放在规则列表的最底部,以便其他规则起作用.所以通过在前面添加:
Thus you need to put this rule at the very bottom of your rule list in order to let the other rules work. So by adding this in front:
$route['mycontroller'] = 'mycontroller';
$route['(:any)'] = 'site/index/$1';
它应该可以正常工作(虽然有点不寻常的方法,但您的全局 any 规则也是如此),因为它会首先检查请求的 URL 是否为 /mycontroller,如果是, 它将调用 myController;否则它会像以前使用 Site 控制器一样运行.
it should work fine (although sligthly unusual approach, but so is your global any rule) as it will first check if the URL requested was /mycontroller and if so, it will call myController; otherwise it will behave like it used to with your Site controller.
这篇关于你如何在 CodeIgniter 中使用多个控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!



大气响应式网络建站服务公司织梦模板
高端大气html5设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)