How do you use multiple controllers in CodeIgniter?

后端 未结 3 1571
别跟我提以往
别跟我提以往 2021-01-13 19:44

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 mul

相关标签:
3条回答
  • 2021-01-13 20:10

    You are routing everything to your Site controller with the routing rule:

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

    With such a catch-all routing rule you will never reach other controllers as for example your Mycontroller.

    0 讨论(0)
  • 2021-01-13 20:20

    The problem is your calling method In your case if you don't have mod_rewrite on you should try

    localhost/website/index.php/mycontroller
    

    if you are able to activate mod_rewrite you can add in your .htaccess the following lines:

    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php/$1
    

    after that you can call your controller like you tried before

    0 讨论(0)
  • 2021-01-13 20:24

    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.

    0 讨论(0)
提交回复
热议问题