将GET参数传递给默认的欢迎控制器

时间:2015-11-26 09:19:34

标签: codeigniter

我在我的服务器上安装了新的Codeigniter安装,我希望能够传递给默认控制器(welcome)GET参数。

例如:

http://www.myserver.com/1234

我希望欢迎控制器上的默认索引函数将获得'1234'作为GET参数,但我不能让它工作任何想法?

这是我的控制器代码:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Welcome extends CI_Controller {

    /**
     * Index Page for this controller.
     *
     * Maps to the following URL
     *      http://example.com/index.php/welcome
     *  - or -
     *      http://example.com/index.php/welcome/index
     *  - or -
     * Since this controller is set as the default controller in
     * config/routes.php, it's displayed at http://example.com/
     *
     * So any other public methods not prefixed with an underscore will
     * map to /index.php/welcome/<method_name>
     * @see http://codeigniter.com/user_guide/general/urls.html
     */
    public function index()
    {
        $this->load->view('welcome_message');

        // echo get parameter here = 1234
    }
}

我的.htaccess代码:

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

4 个答案:

答案 0 :(得分:0)

您的控制器应该是这样的

<?php

defined('BASEPATH') OR exit('No direct script access allowed');

class Welcome extends CI_Controller {

/**
 * Index Page for this controller.
 *
 * Maps to the following URL
 *      http://example.com/index.php/welcome
 *  - or -
 *      http://example.com/index.php/welcome/index
 *  - or -
 * Since this controller is set as the default controller in
 * config/routes.php, it's displayed at http://example.com/
 *
 * So any other public methods not prefixed with an underscore will
 * map to /index.php/welcome/<method_name>
 * @see http://codeigniter.com/user_guide/general/urls.html
 */
public function index($number)
{
    //$this->load->view('welcome_message');

    echo get parameter here = 1234
}

}

答案 1 :(得分:0)

在config.php中,您可以启用查询字符串:

$config['enable_query_strings'] = TRUE;

使用此网址访问该方法:

http://www.myserver.com/?id=1234

我认为这应该有用。

答案 2 :(得分:0)

在你的route.php文件

// Would leave as your default controller
$route['default_controller'] = 'welcome';

// But add get query route here 
$route['number='] = 'welcome/index';

您需要在查询开始时获取,然后在任何其他网址查询之后使用&amp;

然后在控制器上

<?php

defined('BASEPATH') OR exit('No direct script access allowed');

class Welcome extends CI_Controller {

    public function index()
    {
        $this->load->helper('url');

        echo anchor('?number=1234', 'Home', array('target' => '_blank'));

        // http://www.example.com/index.php/?number=1234

        echo '</br>';

        echo $this->input->get('number');

        $this->load->view('welcome_message');
    }
}

当刷新页面时,您应该能够看到可以单击的锚链接,然后将打开新页面并显示数字。

你也可以提出错误不允许uri

然后转到配置并使用?&=

$config['permitted_uri_chars'] = 'a-z 0-9~%.:_\-?&=';

你可以使用uri段

<?php echo $this->uri->segment(1);?>

答案 3 :(得分:0)

使用重映射功能

"ParticipantId" : "56578b12aa9c5817303f306f",