Codeigniter 3.0.3路由问题

时间:2015-12-29 21:28:57

标签: php codeigniter

我有一个名为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编辑需要1参数$id,该参数必须是数字。

现在可以通过此网址访问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";

但它没有任何帮助吗?

2 个答案:

答案 0 :(得分:1)

你有两条相互矛盾的路线:

&#13;
&#13;
$route['wsdl/(:num)'] = "wsdl/wsdl_edit/$1"; 

$route['wsdl/(:any)'] = "wsdl/wsdl_edit/error"; 
&#13;
&#13;
&#13;

首先,你错过了&#39; wsdl_edit&#39;。所以你的路线应该是

&#13;
&#13;
$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.
&#13;
&#13;
&#13;

您也可以随时检查函数中的$ id,删除路径:

&#13;
&#13;
 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
 }
&#13;
&#13;
&#13;

答案 1 :(得分:0)

您忘记了路线规则中的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";
  

注意:路由将按照定义的顺序运行。更高的路线   将始终优先于较低的。