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



我在服务器上安装了新的Codeigniter,我希望能够将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]

您的控制器应该是这样的

<?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';

你需要在查询开始时获取,然后任何其他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

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

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

你可以替代使用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
    }
}

最新更新