你如何在 CodeIgniter 中使用多个控制器?

2022-01-05 00:00:00 php codeigniter controller

我是 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.

相关文章