如何使 CodeIgniter 接受“查询字符串”网址?

发布于 2024-09-02 06:31:46 字数 433 浏览 2 评论 0原文

根据 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 页面未找到。那么我怎样才能启用这个功能呢?

According to CI's docs, CodeIgniter uses a segment-based approach, for example:

example.com/my/group

If I want to find a specific group (id=5), I can visit

example.com/my/group/5

And in the controller, define

function group($id='') {
    ...
    }

Now I want to use the traditional approach, which CI calls "query string" URL. Example:

example.com/my/group?id=5

If I go to this URL directly, I get a 404 page not found. So how can I enable this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

花辞树 2024-09-09 06:31:47

为了可靠地使用查询字符串,我发现您需要在 application/config/config.php 中做 3 件事

  1. 设置 $config['enable_query_strings'] = true;
  2. 再次 在 application/config/config.php 中设置 $config['uri_protocol'] = "PATH_INFO";
  3. 更改您的 .htaccess 以删除 ? (如果存在)在重写规则中

我使用以下内容

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

For reliable use of query strings I've found you need to do 3 things

  1. In application/config/config.php set $config['enable_query_strings'] = true;
  2. Again in application/config/config.php set $config['uri_protocol'] = "PATH_INFO";
  3. Change your .htaccess to remove the ? (if present) in the rewrite rule

I use the following

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
最美的太阳 2024-09-09 06:31:47
//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
}    
//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
}    
十二 2024-09-09 06:31:47

这可能对某些人有帮助;将其放入控制器的构造函数中,以便在逐个控制器的基础上重新填充 $_GET(例如,如果您正在集成依赖于 $_GET 的第三方库 - 例如大多数 PHP OAuth 库)。

parse_str(str_replace($_SERVER['QUERY_STRING'],'',$_SERVER['REQUEST_URI']),$_GET);

This might help some people; put this into your controller's constructor to repopulate $_GET on a controller-by-controller basis (e.g. if you are integrating a third party lib that relies on $_GET - such as most PHP OAuth libraries).

parse_str(str_replace($_SERVER['QUERY_STRING'],'',$_SERVER['REQUEST_URI']),$_GET);
陌生 2024-09-09 06:31:47

你可能会改变
在您的配置文件中添加URI PROTOCOL

  $config['uri_protocol']   = "ORIG_PATH_INFO"; 

它将

  $config['enable_query_strings'] = FALSE;

接受查询字符串并允许您的URL。
为我工作:)

You may change
URI PROTOCOL in your config file to

  $config['uri_protocol']   = "ORIG_PATH_INFO"; 

and

  $config['enable_query_strings'] = FALSE;

It'll accept query strings and allow your URLs.
Worked for me :)

郁金香雨 2024-09-09 06:31:47

Html:

<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>");  
    }
}

Html:

<a href="?accept=1" class="btn btn-sm btn-success">Accept</a>

Controller Function

if ($this->input->get('accept')!='')
{
    $id = $this->input->get('accept', TRUE );
    $this->camprequests->accept($id);
    redirect('controllername/functionname');
}

Model Function

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>");  
    }
}
思念满溢 2024-09-09 06:31:47

修改 application/config.php 行:

$config['enable_query_strings'] = FALSE;

改为 true。您还必须注意其他细节。请参阅此处

Modify application/config.php at the line:

$config['enable_query_strings'] = FALSE;

Make this true instead. There are other details you'll have to pay attention to also. See here.

你的背包 2024-09-09 06:31:47

在 config.php 文件中设置 $config['enable_query_strings'] = TRUE; 后,您可以将基于段的方法与查询字符串结合使用,但前提是您使用 2 或查询字符串中有更多变量(用“&”分隔),如下所示:

example.com/my/group?id=5&var=something

请参阅此答案了解更多信息。

After setting $config['enable_query_strings'] = TRUE; in your config.php file, you can use the segment-based approach in conjunction with query strings, but only if you use 2 or more variables (separated by a "&") in the query string like this:

example.com/my/group?id=5&var=something

See this answer for more information.

独木成林 2024-09-09 06:31:47

CodeIgniter 可以选择支持此功能,可以在 application/config.php 文件中启用该功能。如果打开配置文件,您将看到以下项目:

enter code here $config['enable_query_strings'] = FALSE;

$config['controller_trigger'] = 'c';
$config['function_trigger'] = 'm';

如果将“enable_query_strings”更改为 TRUE,此功能将变为活动状态。

CodeIgniter optionally supports this capability, which can be enabled in your application/config.php file. If you open your config file you'll see these items:

enter code here $config['enable_query_strings'] = FALSE;

$config['controller_trigger'] = 'c';
$config['function_trigger'] = 'm';

If you change "enable_query_strings" to TRUE this feature will become active.

妥活 2024-09-09 06:31:47

这是经过实际测试和确认的,

它适用于您喜欢的任何方法;让您可以自由地混合匹配查询字符串和 / 段方法(与之前的响应相反),

或者您想要使用:(

example.com/my/group/?id=5

请注意后面的 / ?)。或者

 example.com/my/group/5 

(取决于路由器文件中的 url 模式定义)。或者甚至

example.com/index.php/?my/group/?id=5

(虽然这看起来很尴尬)

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

并且在你的 codigniter 的 config/config.php 文件中,设置

$config['uri_protocol'] = 'AUTO';
$config['enable_query_strings'] = TRUE;

This is actually tested and confirmed

It works with any method you like; giving you freedom to mix match the query string and / segment approach (as opposed to the previous responses)

either you want to use:

example.com/my/group/?id=5

(please note the trailing / before ?). OR

 example.com/my/group/5 

(depending on your url pattern definitions in the router file). OR EVEN

example.com/index.php/?my/group/?id=5

(though that looks awkward enough)

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

and in you codigniter's config/config.php file, set

$config['uri_protocol'] = 'AUTO';
$config['enable_query_strings'] = TRUE;
冰火雁神 2024-09-09 06:31:47

我已经尝试过这些步骤,并且在控制器中的分页(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. 

相关链接:
https://codeigniter.com/userguide3/libraries/pagination.html# customizing-the-pagination

甚至,如果您想将自己的特定类添加到(由分页创建的标签)中,则必须添加以下代码以拥有您自己喜欢的分页链接样式。

$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 标记时,这将非常有用。

结果: 5

如您所见,页面链接已添加到此标记。

相关链接:
https://codeigniter.com/userguide3/libraries/pagination。 html#adding-attributes-to-anchors

I have tried these steps and it was correct for me to set a query in pagination (codeigniter 3)

in controller: just add these codes.

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

related link:
https://codeigniter.com/userguide3/libraries/pagination.html#customizing-the-pagination

Even, if you want to add your own specific class to the (a tags that are created by pagination), you must add the following code to have your own favorite style for pagination links.

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

This will be very useful when we want to add page-link class as boostrap style to the a tag.

result: <a href="home?action=questions&page=12" class="page-link" data-ci-pagination-page="5" dideo-checked="true">5</a>

as you see page-link was added to this a tag.

related link:
https://codeigniter.com/userguide3/libraries/pagination.html#adding-attributes-to-anchors

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文