<small id='7CVhK'></small><noframes id='7CVhK'>

<legend id='7CVhK'><style id='7CVhK'><dir id='7CVhK'><q id='7CVhK'></q></dir></style></legend>
    <bdo id='7CVhK'></bdo><ul id='7CVhK'></ul>

      <tfoot id='7CVhK'></tfoot>

      1. <i id='7CVhK'><tr id='7CVhK'><dt id='7CVhK'><q id='7CVhK'><span id='7CVhK'><b id='7CVhK'><form id='7CVhK'><ins id='7CVhK'></ins><ul id='7CVhK'></ul><sub id='7CVhK'></sub></form><legend id='7CVhK'></legend><bdo id='7CVhK'><pre id='7CVhK'><center id='7CVhK'></center></pre></bdo></b><th id='7CVhK'></th></span></q></dt></tr></i><div id='7CVhK'><tfoot id='7CVhK'></tfoot><dl id='7CVhK'><fieldset id='7CVhK'></fieldset></dl></div>

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

        时间:2023-10-11
        <i id='R3H5p'><tr id='R3H5p'><dt id='R3H5p'><q id='R3H5p'><span id='R3H5p'><b id='R3H5p'><form id='R3H5p'><ins id='R3H5p'></ins><ul id='R3H5p'></ul><sub id='R3H5p'></sub></form><legend id='R3H5p'></legend><bdo id='R3H5p'><pre id='R3H5p'><center id='R3H5p'></center></pre></bdo></b><th id='R3H5p'></th></span></q></dt></tr></i><div id='R3H5p'><tfoot id='R3H5p'></tfoot><dl id='R3H5p'><fieldset id='R3H5p'></fieldset></dl></div>
          <tbody id='R3H5p'></tbody>

      2. <legend id='R3H5p'><style id='R3H5p'><dir id='R3H5p'><q id='R3H5p'></q></dir></style></legend>
        1. <tfoot id='R3H5p'></tfoot>

                <bdo id='R3H5p'></bdo><ul id='R3H5p'></ul>

                  <small id='R3H5p'></small><noframes id='R3H5p'>

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

                  问题描述

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

                  上一篇:Symfony BinaryFileResponse 设置文件名 下一篇:Codeigniter 与本地主机 (XAMPP) 中的路径有关的问题

                  相关文章

                  <tfoot id='Dwnay'></tfoot>
                • <legend id='Dwnay'><style id='Dwnay'><dir id='Dwnay'><q id='Dwnay'></q></dir></style></legend>

                  <i id='Dwnay'><tr id='Dwnay'><dt id='Dwnay'><q id='Dwnay'><span id='Dwnay'><b id='Dwnay'><form id='Dwnay'><ins id='Dwnay'></ins><ul id='Dwnay'></ul><sub id='Dwnay'></sub></form><legend id='Dwnay'></legend><bdo id='Dwnay'><pre id='Dwnay'><center id='Dwnay'></center></pre></bdo></b><th id='Dwnay'></th></span></q></dt></tr></i><div id='Dwnay'><tfoot id='Dwnay'></tfoot><dl id='Dwnay'><fieldset id='Dwnay'></fieldset></dl></div>

                  1. <small id='Dwnay'></small><noframes id='Dwnay'>

                      <bdo id='Dwnay'></bdo><ul id='Dwnay'></ul>