当我以不存在的路线访问Class时,如何在codeIgniter中设置默认功能? [英] How to set default function in codeIgniter when I visit Class with non-existent route?
问题描述
像这样的控制器:
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找不到
If url is http://host/app/Abc/index
it get function index
If url is http://host/app/Abc/f1
it get function f1
If url is http://host/app/Abc
it get function index because it is default
But if url is http://host/app/Abc/f2
it print 404 not found
我希望如果url是 http:// host / app / Abc / f2
索引功能。
如果无法执行此操作,我想自动添加新功能,该怎么办?
I expected that if url is http://host/app/Abc/f2
it can turn to index function.
If can't do this,I want to add new function automatically,What should I do?
编辑
我只想在特定的类中使用它,我可以编辑全局路由吗?
EDIT
I want to use it in just a specific class, can I edit global routing? How?
推荐答案
您可以通过两种方式进行操作:首先编辑routes.php文件,然后将404_override更改为控制器功能,这将重定向您的所有对该控制器功能的404请求
Two ways you can do it first edit routes.php file and change 404_override to controller function this will redirect all your 404 request to that controller function
form
$route['404_override'] = 'welcome';
to
$route['404_override'] = 'ABC/index';
第二个选项在控制器内,您可以使用_remap方法/函数来检查函数/方法是否存在。控制器将是这样
second option is within controller you can use _remap method/function to check either function/method exist or not. controller will be like this
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(){...}
}
这篇关于当我以不存在的路线访问Class时,如何在codeIgniter中设置默认功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!