【问题标题】:Codeigniter 3.0.3 routes issueCodeigniter 3.0.3 路由问题
【发布时间】:2015-12-29 21:28:56
【问题描述】:

我有一个名为 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";

但它没有任何帮助吗?

【问题讨论】:

    标签: php codeigniter


    【解决方案1】:

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

    $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
     }

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        猜你喜欢
        • 2011-09-16
        • 2012-04-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多