简体   繁体   English

使用post和get params的Codeigniter路由

[英]Codeigniter Routing using post and get params

I have the following in my config/routes.php file: 我的config / routes.php文件中包含以下内容:

$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'. 当访问以下URL时,此方法将按预期工作: mysite.com/user/id ,仪表板控制器中的函数被调用,第一个参数为“ user”,第二个参数为“ 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'. 但是,当通过post并获取诸如mysite.com/user/id?page=2之类的值时,该函数似乎忽略了post参数,并且第一个(也是唯一一个)参数为'2'。

My config.php file has following values: 我的config.php文件具有以下值:


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

and .htaccess: 和.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? 我在路线状况中是否缺少某些东西?

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. 这绝对是您的自定义路线,能否请您尝试yoursite.com/user/id/?page=2,因为我认为字符串id?page = 2被作为一个参数传递了。 You can also do debugging with printing out the array of passed url keys: 您还可以通过打印出传递的url键数组来进行调试:

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. 您还可以看到以下答案:如果需要, 在codeigniter中处理有关扩展核心URI类的url中的问号 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. 我希望这会有所帮助,否则更多信息会有所帮助,例如,当您使用诸如site.com/user/id之类的常规请求时以及在执行site.com/user/id时,控制器中传递的url键是什么? page = 2个请求。

****** 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: ******编辑**********如果您使用的是CI 1.7.1或1.7.2,那么您将获得[1] =>页面,因为在_fetch_uri_string方法中,您将看到以下内容:

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 由于您具有site.com/user/id/?page=2,因此这意味着:$ _GET ['page'] = 2,page是$ _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 请使用上面的链接,了解如何在您的应用程序中扩展此类并在此处添加您自己的逻辑,例如,类似的内容适用于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)
{

}

Hope this helps 希望这可以帮助

uri segments wont help you here. uri细分不会在这里为您提供帮助。 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] 并输出以下内容: user, id, [2]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM