简体   繁体   English

CodeIgniter中的默认控制器

[英]Default Controller in CodeIgniter

I am wondering if there is any other configuration options for a default controller. 我想知道默认控制器是否还有其他配置选项。

For example - if I have a controller called "site" and I set the default controller in the following file: application/config/routes.php to: 例如-如果我有一个名为“ site”的控制器,并且在以下文件中将默认控制器设置为:application / config / routes.php到:

$route['default_controller'] = "site";

I should be able to go to http://localhost and that brings up the index(); 我应该能够去http://localhost并显示index(); function in the site controller. 功能在站点控制器中。

However, if I try to do go to http://localhost/index.php/index2 to load the index2(); 但是,如果尝试这样做,请转到http://localhost/index.php/index2加载index2(); function I get a 404 error. 功能我收到404错误。 If I change the URL to http://localhost/index.php/site/index2 it works fine - but I thought already set the default controller. 如果我将URL更改为http://localhost/index.php/site/index2它可以正常工作-但我认为已经设置了默认控制器。 Is there any way around this? 有没有办法解决?

唯一的方法是手动编写路由规则:

$route['index2'] = "site/index2";

You cannot do that. 你不能这样做。

The default_controller is only for URLs without any URI parameter. default_controller 适用于没有任何 URI参数的URL。 How could CodeIgniter distinguish between a method name and a controller name? CodeIgniter如何区分方法名称和控制器名称?

What you can do: define a redirect inside your 404 document or directly map your 404 document to index.php 您可以执行的操作:在404文档中定义重定向或直接将404文档映射到index.php

没有CI不能像第一个参数必须是控制器的名称那样工作,因此在这种情况下,您将必须创建一个称为“ index2”的控件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM