如果您想在 codeigniter url 中使用冒号 (:) 字符,您可以从以下说明中扩展 CI_Router 和 CI_URI 类:
首先,在“application/core”文件夹中新建文件“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;
}
}
}
}
其次,在“application/core”文件夹中创建另一个文件“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 存储库中找到它:Codeigniter Colon URI