我有一个名为Wsdl:的控制器
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Wsdl extends MY_Controller {
public function wsdl() {
}
public function wsdl_edit($id) {
}
}
wsdl编辑采用CCD_ 1参数CCD_。
现在可以通过以下网址访问wsdl_edit
方法:mywebsite.com/admin/wsdl/wsdl_edit/1
当1
丢失时,我显示一个页面错误。当某些东西没有像mywebsite.com/admin/wsdl/wsdl_edit/xx
那样使用数字时我显示了一个错误。我正试图在路由配置中这样做:
$route['wsdl/(:num)'] = "wsdl/wsdl_edit/$1";
$route['wsdl/(:any)'] = "wsdl/wsdl_edit/error";
$route['wsdl'] = "wsdl/wsdl_edit/error";
但它不起作用,有什么帮助吗?
您有两个相互矛盾的路由:
$route['wsdl/(:num)'] = "wsdl/wsdl_edit/$1";
$route['wsdl/(:any)'] = "wsdl/wsdl_edit/error";
首先,您错过了"wsdl_edit"。所以你的路线应该是
$route['wsdl/wsdl_edit/(:num)'] = "wsdl/wsdl_edit/$1";
//Works if an integer is the parameter after wsdl/wsdl_edit/
$route['wsdl/wsdl_edit/(:any)'] = "wsdl/wsdl_edit/error";
//Works if anything is the parameter after wsdl/wsdl_edit/ including integer.
//This route will override the above rule and will be executed.
你也可以在函数中检查这个$id,去掉路由:
public function wsdl_edit($id) {
# Check if your variable is an integer
if( filter_var($id, FILTER_VALIDATE_INT) !== false ){
redirect('error.php') // when $id is not an integer.
}
}
else{ //Your desired action
}
您忘记了路由规则中的wsdl_edit
方法
$route['wsdl/wsdl_edit/(:num)'] = "wsdl/wsdl_edit/$1";
$route['wsdl/wsdl_edit/(:any)'] = "wsdl/wsdl_edit/error";
或者,如果您喜欢使用正则表达式
$route['wsdl/wsdl_edit/([0-9]+)'] = "wsdl/wsdl_edit/$1";
$route['wsdl/wsdl_edit/.+'] = "wsdl/wsdl_edit/error";
注意:路由将按定义的顺序运行。较高的路线将始终优先于较低的。