Wednesday, March 9, 2011

让 CodeIgniter 支持 $_GET

方法一:
CI默认过滤了$_GET

需要传递get参数时一般直接 /参数一/参数二
详见手册说明:http://codeigniter.org.cn/user_guide/general/controllers.html#passinguri

但是有时候需要传递很长的复杂的url,比如常用的 http://www.nicewords.cn/index.php/controller/method/?backURL=http://baidu.com/blog/hi

这时 这种模式就行不通了。参数中本身的/会与默认的分隔符冲突

解决方案:

1) 在config.php 中,将‘uri_protocol’ 设置为 ‘PATH_INFO’.
PHP
$config['uri_protocol'] = "PATH_INFO";
复制代码


2) 在需要使用$_GET的之前加:
PHP
parse_str($_SERVER['QUERY_STRING'], $_GET);
复制代码


这样,形如 index.php/blog/list?parm=hello&page=52 就可以运行了。

官网说明:
http://codeigniter.com/wiki/QUERY_STRING_GET/
也许您还可能感兴趣:

* • PHP搭建基于CI框架的REST服务架构


方法二:
我在官網討論區看到一篇很不錯的解法 http://codeigniter.com/forums/viewthread/68698/#389281
他只有二個步驟

1.config.php的設定
$config['uri_protocol'] = "REQUEST_URI";
$config['enable_query_strings'] = TRUE;


2.將以下程式存檔,檔名叫MY_URI.php
存檔路徑 自己的system/application/libraries/ folder

PHP

class MY_URI extends CI_URI {

function MY_URI() {
parent::CI_URI();
}

// --------------------------------------------------------------------

/**
* Override the _parse_request_uri method so it allows query strings through
*
* @access private
* @return string
*/
function _parse_request_uri()
{
if ( ! isset($_SERVER['REQUEST_URI']) OR $_SERVER['REQUEST_URI'] == '')
{
return '';
}

$uri = explode("?",$_SERVER['REQUEST_URI']); // This line is added to the original
$request_uri = preg_replace("|/(.*)|", "\\1", str_replace("\\", "/", $uri[0])); // This line changed
// Everything else is just the same

if ($request_uri == '' OR $request_uri == SELF)
{
return '';
}

$fc_path = FCPATH;
if (strpos($request_uri, '?') !== FALSE)
{
$fc_path .= '?';
}

$parsed_uri = explode("/", $request_uri);

$i = 0;
foreach(explode("/", $fc_path) as $segment)
{
if (isset($parsed_uri[$i]) AND $segment == $parsed_uri[$i])
{
$i++;
}
}

$parsed_uri = implode("/", array_slice($parsed_uri, $i));

if ($parsed_uri != '')
{
$parsed_uri = '/'.$parsed_uri;
}

return $parsed_uri;
}

}

?>
复制代码

No comments: