Controller 是这样的:
class Abc extends CI_controller{
public function index(){...}
public function f1(){...}
}
如果 url 是 http://host/app/Abc/index
它得到函数索引
如果 url 是 http://host/app/Abc/f1
它得到函数 f1
如果 url 是 http://host/app/Abc
它会得到函数索引,因为它是默认的
但是如果 url 是 http://host/app/Abc/f2
它会打印 404 not found
我希望如果 url 是 http://host/app/Abc/f2
它可以转向索引功能。
如果做不到,我想自动添加新功能,我该怎么办?
编辑
我只想在特定类中使用它,我可以编辑全局路由吗?怎么办?
最佳答案
您可以通过两种方式首先编辑 routes.php 文件并将 404_override 更改为 Controller 函数,这会将您的所有 404 请求重定向到该 Controller 函数
form
$route['404_override'] = 'welcome';
to
$route['404_override'] = 'ABC/index';
第二个选项在 Controller 中,您可以使用 _remap 方法/函数来检查函数/方法是否存在。 Controller 将是这样的
class Abc extends CI_controller{
function _remap($method_name = 'index'){
if(!method_exists($this, $method_name)){
$this->index();
}
else{
$this->{$method_name}();
}
}
public function index(){...}
public function f1(){...}
}
关于php - 当我使用不存在的路由访问类时,如何在 codeIgniter 中设置默认函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25176713/