如何在CodeIgniter中使用多个控制器? [英] How do you use multiple controllers in CodeIgniter?

查看:125
本文介绍了如何在CodeIgniter中使用多个控制器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是CodeIgniter的新手,我刚刚浏览了他们的用户指南,帮助我了解如何使用多个控制器。



我想出了如何使用他们的示例加载多个页面。这是我的默认控制器,名为Site.php:

  class Site extends CI_Controller {

public function index($ page ='home'){
if(!file_exists(APPPATH。'/ views /'.$ page。'。php')){
//糟糕,一个页面!
show_404();
}

$ data ['title'] = ucfirst($ page);

$ this-> load-> view('header',$ data);
$ this-> load-> view($ page,$ data);
$ this-> load-> view('footer',$ data);

}
}

我的routes.php:

  $ route ['default_controller'] ='site' 
$ route ['(:any)'] ='site / index / $ 1';

我有一些视图,当我去 localhost /






现在,为了进一步了解控制器和URI路由。我创建了一个新的控制器, application / controllers / MyController.php ,它编码如下:

  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 一样愚蠢,但当然不起作用。





使用多个控制器的正确方法是什么?我在这里做错了什么?请帮助我学习,谢谢!



(此外,这是我在StackOverflow的第一个正确的问题,如果我做了什么错误或格式错误,请让我知道!)

解决方案

问题是这个规则: p>

  $ route ['(:any)'] ='site / index / $ 1'; 

截取一切 ...并将其传递给 Site 控制器作为 index 方法的参数。因此,如果您调用:

  / mycontroller 

它实际上会映射到:

  / site / index / mycontroller 

路由发生在应用程序甚至查看控制器之前,并且路由规则被考虑以便它们被写入。 / p>

因此,您需要将此规则放在规则列表的最底部,以便让其他规则起作用。因此,通过在前面添加:

  $ route ['mycontroller'] ='mycontroller'; 
$ route ['(:any)'] ='site / index / $ 1';

它应该工作正常(虽然sligthly不寻常的方法,但你的全局任何规则),因为它将首先检查所请求的URL是否 / mycontroller ,如果是,将调用 myController ;否则它会像您以前的 Site 控制器一样工作。


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.

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);

    }
}

This all works well and good with my routes.php:

$route['default_controller'] = 'site';
$route['(:any)'] = 'site/index/$1';

I have a few views that do load correctly when I go to localhost/website/index.php/about, for example.


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

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.


What is the correct way to use multiple controllers? What have I done wrong here? Please assist me in my learning, thank you!

(Also, this is my first proper question on StackOverflow, if I've done anything wrong or formatted something incorrectly, please let me know!)

解决方案

The problem is that this rule:

$route['(:any)'] = 'site/index/$1';

intercepts everything... and passes it to the Site controller, as an argument to the index method. So if you call:

/mycontroller

it will actually map to:

/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';

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中使用多个控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆