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

Codeigniter 将GET参数传递给默认的欢迎控制器,codeigniter,Codeigniter,我已经在我的服务器上安装了新的Codeigniter安装,我希望能够传递到默认控制器(welcome)获取参数 例如: 我希望欢迎控制器上的默认索引函数将获取'1234'作为get参数,但我不能让它工作任何想法 这是我的控制器代码: <?php defined('BASEPATH') OR exit('No direct script access allowed'); class Welcome extends CI_Controller { /** * Inde

我已经在我的服务器上安装了新的Codeigniter安装,我希望能够传递到默认控制器(welcome)获取参数

例如:

我希望欢迎控制器上的默认索引函数将获取'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
    }
}

你的控制器应该是这样的

<?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
}

}

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

$config['enable_query_strings'] = TRUE;
使用此url访问方法的权限:

http://www.myserver.com/?id=1234
我认为这应该行得通。

在route.php文件中

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

// But add get query route here 
$route['number='] = 'welcome/index';
您需要在查询get开始时使用,然后在这之后,任何其他url查询使用&

然后在控制器上

<?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段

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

使用重新映射功能

<?php

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

class Welcome extends CI_Controller {

    public function _remap($method, $params = array())
    {

        if (method_exists($this, $method))
            return call_user_func_array(array($this, $method), $params);
        else
            return call_user_func_array(array($this, 'index'), $params);    
    }
    public function index($number)
    {
        //$this->load->view('welcome_message');

        echo get parameter here = 1234
    }

}

是否可以在不启用$config['enable_query_strings']=TRUE的情况下执行此操作?您应该这样调用控制器:对不起,您应该这样调用控制器:localhost:8000/projectname/classname/controllername/{parameters}
<?php echo $this->uri->segment(1);?>
<?php

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

class Welcome extends CI_Controller {

    public function _remap($method, $params = array())
    {

        if (method_exists($this, $method))
            return call_user_func_array(array($this, $method), $params);
        else
            return call_user_func_array(array($this, 'index'), $params);    
    }
    public function index($number)
    {
        //$this->load->view('welcome_message');

        echo get parameter here = 1234
    }

}