使用post和get params的Codeigniter路由

时间:2011-03-24 23:00:22

标签: php codeigniter codeigniter-url

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

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

在访问以下网址时,这可以正常工作: mysite.com/user/id ,调用仪表板控制器中的功能,第一个参数是“用户”和第二个“ID”。但是,当通过帖子并获取 mysite.com/user/id?page=2 等值时,该函数似乎忽略了post params并且第1个(也是唯一的)param为'2'。< / p>

我的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] 

我的路线状况是否遗漏了什么?

2 个答案:

答案 0 :(得分:1)

这绝对是您的自定义路线,请您试试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键是什么? page = 2个请求。

******编辑********** 如果您使用的是CI 1.7.1或1.7.2,那么您将获得[1] =&gt;页面,因为在_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)
{

}

希望这有帮助

答案 1 :(得分:0)

uri片段在这里不会帮到你。 你应该用

获得页面的价值
$page = $this->input->get('page');

你的方法界面是什么样的?

我刚试过这个

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

并输出:user, id, [2]