Php 如何让CodeIgniter接受;查询字符串";网址?
根据CI的文档,CodeIgniter使用基于段的方法,例如:Php 如何让CodeIgniter接受;查询字符串";网址?,php,codeigniter,codeigniter-url,pagination,pagination-bootstrap,pagination-in-query-based-paged,Php,Codeigniter,Codeigniter Url,Pagination,Pagination Bootstrap,Pagination In Query Based Paged,根据CI的文档,CodeIgniter使用基于段的方法,例如: example.com/my/group 如果我想找到一个特定的组(id=5),我可以访问 example.com/my/group/5 在控制器中,定义 function group($id='') { ... } 现在我想使用传统的方法,CI称之为“查询字符串”URL。例如: example.com/my/group?id=5 如果我直接访问这个URL,我会得到一个未找到的404页面。那么我如何启用它呢?
example.com/my/group
如果我想找到一个特定的组(id=5),我可以访问
example.com/my/group/5
在控制器中,定义
function group($id='') {
...
}
现在我想使用传统的方法,CI称之为“查询字符串”URL。例如:
example.com/my/group?id=5
如果我直接访问这个URL,我会得到一个未找到的404页面。那么我如何启用它呢?在以下行中修改
application/config.php
:
$config['enable_query_strings'] = FALSE;
相反,要做到这一点。你还需要注意其他一些细节。请参阅。在设置
$config['enable\u query\u strings']=TRUE之后
在config.php文件中,您可以将基于段的方法与查询字符串结合使用,但前提是在查询字符串中使用两个或多个变量(以“&”分隔),如下所示:
example.com/my/group?id=5&var=something
更多信息。要可靠使用查询字符串,我发现您需要做3件事
application/config/config.php中
set$config['enable_query_strings']=true代码>
application/config/config.php
set$config['uri\u protocol']=“PATH\u INFO”代码>
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
这可能会帮助一些人;将其放入控制器的构造函数中,以逐个控制器重新填充$\u-GET(例如,如果您正在集成依赖$\u-GET的第三方库,例如大多数PHP OAuth库)
CodeIgniter可选地支持此功能,可以在application/config.php文件中启用此功能。如果打开配置文件,您将看到以下项目:
enter code here $config['enable_query_strings'] = FALSE;
$config['controller_trigger']='c';
$config['function_trigger']='m'
如果将“启用查询字符串”更改为TRUE,此功能将激活。您可以更改
//Add this method to your (base) controller :
protected function getQueryStringParams() {
parse_str($_SERVER['QUERY_STRING'], $params);
return $params;
}
// Example : instagram callback action
public function callback()
{
$params = $this->getQueryStringParams();
$code = !empty($params['code']) ? $params['code'] : '';
if (!empty($code))
{
$auth_response = $this->instagram_api->authorize($code);
// ....
}
// .... handle error
}
URI协议
在config文件中
$config['uri_protocol'] = "ORIG_PATH_INFO";
及
它将接受查询字符串并允许您的URL。
为我工作:)这实际上是经过测试和确认的
它适用于任何你喜欢的方法;允许您自由混合匹配查询字符串和/或段方法(与前面的响应相反)
您可以选择使用:
example.com/my/group/?id=5
(请注意尾随/前?)。或
(取决于路由器文件中的url模式定义)。甚至
(尽管这看起来很尴尬)
在codigniter的config/config.php文件中,设置
$config['uri_protocol'] = 'AUTO';
$config['enable_query_strings'] = TRUE;
Html:
<a href="?accept=1" class="btn btn-sm btn-success">Accept</a>
模型功能
public function accept($id)
{
$data = array('status'=>'1');
$this->db->where('id','1');
if($this->db->update('tablename',$data)) {
$this->session->set_flashdata("accpeted","<div class='col-sm-12 alert alert-success'>Accpeted successfully.</div>");
} else {
$this->session->set_flashdata("accpeted","<div class='col-sm-12 alert alert-success'>Error..</div>");
}
}
公共函数接受($id)
{
$data=array('status'=>'1');
$this->db->where('id','1');
如果($this->db->update($tablename',$data)){
$this->session->set_flashdata(“已接受”,“已成功接受”);
}否则{
$this->session->set_flashdata(“已接受”、“错误…”);
}
}
我已经尝试了这些步骤,在分页中设置查询是正确的(codeigniter 3)
在控制器中:只需添加这些代码
$config['enable_query_strings'] = TRUE;
$config['page_query_string'] = TRUE;
$config['query_string_segment'] = 'page'; // you can jot down what you want instead of page in this one. and you could get by this name your $_GET method. So, if you don't use this line, it will automatically create a query with "per_page" and this will be your query.
相关链接:
甚至,如果您想将自己的特定类添加到(通过分页创建的标记),则必须添加以下代码以拥有自己喜欢的分页链接样式
$config['attributes'] = array('class' => 'page-link'); // this is the example of the above and you can write what you want instead of 'page-link' as the value of class.
当我们想将页面链接类作为boostrap样式添加到a标记时,这将非常有用
结果:
正如您所看到的,页面链接被添加到这个标签中
相关链接:
该配置似乎指示URL解释器对其控制器/方法参数使用查询字符串而不是路径段。我不确定发问者想要的是什么。我已经重读了这个问题,这个问题似乎是“如何启用查询字符串URL?我同意您的看法。此设置仅允许您在查询字符串URL中使用控制器和方法参数。我仍然收到404错误。CodeIgniter位于my.server.com/project/ci
,还有my.server.com/project/ci/index.php
,my.server.com/project/ci/.htaccess
,还有my.server.com/project/ci/system/application
。我的坏。我刚意识到你的要求。不能混合和匹配查询字符串/段方法。如果您想使用段访问控制器/方法,您还需要通过该方法传入idexample.com/my/group/5/
。或者您需要包含类似于$id=$this->input->get('id')如果您真的想从查询字符串中获取它,请在方法顶部的code>。或者您可以启用查询字符串,并使用它来选择控制器和方法my.server.com/project/ci/index.php?c=my&m=group&id=5
。谢谢,我将$id=$this->input->get('id')
放在控制器的开头,并设置$config['uri\u protocol']=“query\u STRING”。但是example.com/my/group?id=5
仍然不起作用。我得到了404错误。还应该做什么?您需要修改应用程序/config/config.php
行$config['enable\u query\u strings']=FALSE代码>为true
。将uri_协议保留在auto
。你是说如果我只有一个参数(id),那么我就不能使用查询字符串URL吗?@Peter:如果你希望控制器和方法作为uri参数(而不是查询字符串中的变量),那么是的-根据一些简短的测试,似乎需要在查询字符串中附加2个或更多的变量(我不知道为什么)。如果只有一个变量存在,它会抛出404错误。我用parse_str($\u SERVER['QUERY_STRING'],$\u GET)尝试了它;这不起作用。但这一个为我做了。谢谢!请在复制和粘贴时引用您的源代码。太好了!这是一个完美的解决方案,您希望将查询字符串的使用限制为仅限于那些迫使您使用它们的api。这实际上是一个侵入性较小且有效的解决方案,您可以将所有段放在同一侧
$config['uri_protocol'] = 'AUTO';
$config['enable_query_strings'] = TRUE;
<a href="?accept=1" class="btn btn-sm btn-success">Accept</a>
if ($this->input->get('accept')!='')
{
$id = $this->input->get('accept', TRUE );
$this->camprequests->accept($id);
redirect('controllername/functionname');
}
public function accept($id)
{
$data = array('status'=>'1');
$this->db->where('id','1');
if($this->db->update('tablename',$data)) {
$this->session->set_flashdata("accpeted","<div class='col-sm-12 alert alert-success'>Accpeted successfully.</div>");
} else {
$this->session->set_flashdata("accpeted","<div class='col-sm-12 alert alert-success'>Error..</div>");
}
}
$config['enable_query_strings'] = TRUE;
$config['page_query_string'] = TRUE;
$config['query_string_segment'] = 'page'; // you can jot down what you want instead of page in this one. and you could get by this name your $_GET method. So, if you don't use this line, it will automatically create a query with "per_page" and this will be your query.
$config['attributes'] = array('class' => 'page-link'); // this is the example of the above and you can write what you want instead of 'page-link' as the value of class.