此路由
$route['api2/(:any)'] = "api2/api2/$1/";
匹配这些URL:
/api2/pictures/alpha_string
/api2/pictures/picture_id:alpha_string
但不是这个:
/api2/pictures/picture_id:55
不同的是,我使用的是":"后面的数字。
如何使其与上一个URL匹配?
感谢
如果你想在代码点火器url中使用冒号(:)字符,你可以扩展CI_Router&CI_URI根据以下说明分类:
首先,创建新文件"MY_Router.php";在"应用程序/核心";文件夹:
<?php defined('BASEPATH') or exit('No direct script access allowed');
class MY_Router extends CI_Router
{
protected function _set_request($segments = [])
{
parent::_set_request($segments);
$this->_set_params($segments);
}
protected function _set_params($params = [])
{
foreach($params as $key => $value) {
if( FALSE !== strpos($value, ':') && preg_match('/^([a-z0-9'-_]+)':(.*)$/iu',$value,$m) ) {
$this->uri->params[$m[1]] = $m[2];
unset($params[$key]);
continue;
}
}
}
}
第二,创建另一个文件"MY_URI.php";在"应用程序/核心";文件夹:
<?php defined('BASEPATH') or exit('No direct script access allowed');
class MY_URI extends CI_URI
{
public $params = [];
public function param($n, $no_result = NULL)
{
return isset($this->params[$n]) ? $this->params[$n] : $no_result;
}
}
function param($n, $no_result = NULL)
{
return get_instance()->uri->param($n, $no_result);
}
仅此而已,现在您可以通过以下示例获得分离的url参数:
$param1 = $this->uri->param('param1') // output = value1
$param2 = $this->uri->param('param2') // output = value2
$param3 = $this->uri->param('param3') // output = null
$param4 = $this->uri->param('param4', 1) // output = 1
$param1 = param('param1') // output = value1
$param2 = param('param2') // output = value2
$param3 = param('param3') // output = null
$param4 = param('param4', 1) // output = 1
此外,我已经编写了一个小库来处理在url中使用冒号的问题,您可以从github存储库中找到它:Codeigner colon URI