Codeigniter 使用 post 和 get 参数进行路由

发布于 2024-10-26 06:13:09 字数 695 浏览 1 评论 0原文

我的 config/routes.php 文件中有以下内容:

$route['(:any)'] = "dashboard/index/$1";

访问以下网址时,这将按预期工作:< strong>mysite.com/user/id,调用仪表板控制器中的函数,第一个参数是“user”,第二个参数是“id”。但是,当传递 post 并获取诸如 mysite.com/user/id?page=2 之类的值时,该函数似乎会忽略 post 参数,并且第一个(也是唯一的)参数是“2”。

我的 config.php 文件具有以下值:


$config[‘uri_protocol’]  = “AUTO”;
$config[‘enable_query_strings’] = TRUE;

和.htaccess:


RewriteEngine on
RewriteCond $1 !^(index.php|resources|robots.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA] 

我的路线条件中是否缺少某些内容?

I have the following in my config/routes.php file:

$route['(:any)'] = "dashboard/index/$1";

This works as expected when visiting the following url: mysite.com/user/id, the function in the dashboard controller is called and the 1st param is 'user' and 2nd 'id'. However when passing through post and get values such as mysite.com/user/id?page=2 the function appears to ignore the post params and the 1st (and only) param is '2'.

My config.php file has following values:


$config[‘uri_protocol’]  = “AUTO”;
$config[‘enable_query_strings’] = TRUE;

and .htaccess:


RewriteEngine on
RewriteCond $1 !^(index.php|resources|robots.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA] 

Am I missing something in my route condition?

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

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

发布评论

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

评论(2

暗藏城府 2024-11-02 06:13:09

这绝对是您的自定义路线,您可以尝试 yoursite.com/user/id/?page=2 因为我认为字符串 id?page=2 作为一个参数传递。
您还可以通过打印出传递的 url 键的数组来进行调试:

echo '<pre>';
print_r($this->uri->segment_array());
echo '</pre>'

并且您将获得传递给控制器​​操作的所有参数。您还可以看到这个答案: Handling Question mark in url in codeigniter 关于扩展核心 URI 类(如果需要)。
我希望这会有所帮助,否则更多信息会有所帮助,例如,当您使用 site.com/user/id 等普通请求以及执行 site.com/user/id 时,控制器中传递的 url 键是什么?页=2 个请求。

****** 编辑 **********
如果您使用的是 CI 1.7.1 或 1.7.2,那么您将得到 [1] => page 因为在 _fetch_uri_string 方法中你有这样的东西:

if (is_array($_GET) && count($_GET) == 1 && trim(key($_GET), '/') != '')
{
     $this->uri_string = key($_GET);
     return;
}

因为你有 site.com/user/id/?page=2 这意味着: $_GET['page'] = 2 并且 page 是 $_GET 中键的名称

请使用上面关于如何在应用程序中扩展此类的链接,并在此处添加您自己的逻辑,例如类似的内容适用于 1.7.1 或 1.7.2

if (is_array($_GET) && count($_GET) == 1 && trim(key($_GET), '/') != '')
{
     //Comment this line cause you don't need it
     //$this->uri_string = key($_GET);

     //fetch the current page number if there is one
     $page_number = (int) $_GET['page'];

     //Set the uri_string here with the proper id and pass the id (which you should get here too) and the page number as second parameter then just return; to stop execution further
     $this->uri_string = 'dashboard/index/id/'.$page_number
     return;
}

//In your dashboard controller:
//Set default values to 0 and process further
public function index($id=0, $page_number=0)
{

}

希望这会有所帮助

It's definitely your custom route, could you please try yoursite.com/user/id/?page=2 cause I think that the string id?page=2 gets passed as one parameter.
You can also do debugging with printing out the array of passed url keys:

echo '<pre>';
print_r($this->uri->segment_array());
echo '</pre>'

and you will get all params passed to your controller action. You can also see this answer: Handling question mark in url in codeigniter about extending the core URI class if that is needed.
I hope this helps, otherwise more info will be helpful, for example what are the url keys passed in your controller when you are using normal request like site.com/user/id and when you are doing site.com/user/id?page=2 requests.

****** EDIT **********
If you are using CI 1.7.1 or 1.7.2 then you are getting [1] => page because in the _fetch_uri_string method you have something like this:

if (is_array($_GET) && count($_GET) == 1 && trim(key($_GET), '/') != '')
{
     $this->uri_string = key($_GET);
     return;
}

Since you have site.com/user/id/?page=2 this means: $_GET['page'] = 2 and page is the name of the key in $_GET

Please use the link above about how to extend this class in your application and add your own logic here, for example something like this would work for 1.7.1 or 1.7.2

if (is_array($_GET) && count($_GET) == 1 && trim(key($_GET), '/') != '')
{
     //Comment this line cause you don't need it
     //$this->uri_string = key($_GET);

     //fetch the current page number if there is one
     $page_number = (int) $_GET['page'];

     //Set the uri_string here with the proper id and pass the id (which you should get here too) and the page number as second parameter then just return; to stop execution further
     $this->uri_string = 'dashboard/index/id/'.$page_number
     return;
}

//In your dashboard controller:
//Set default values to 0 and process further
public function index($id=0, $page_number=0)
{

}

Hope this helps

野侃 2024-11-02 06:13:09

uri 段在这里对你没有帮助。
获取 page 的值

$page = $this->input->get('page');

您应该通过您的方法接口

?我刚刚尝试过

public function index($one = '', $two = '')
{
    echo $one.', '.$two.', ';
    echo '['.$this->input->get('page').']';
}

,它输出:user, id, [2]

uri segments wont help you here.
you should get the value of page with

$page = $this->input->get('page');

what does your method interface look like?

I just tried this

public function index($one = '', $two = '')
{
    echo $one.', '.$two.', ';
    echo '['.$this->input->get('page').']';
}

and it output this: user, id, [2]

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