在控制器的索引函数中获取uri段 - 代码点火器


Get uri segment in index function of controller - Codeigniter

我有一个网址

www.xample.com/app/pay/11

其中工资是我的控制者。控制器定义为

class Pay extends CI_Controller
{
    function __construct()
    {
        parent::__construct();
        $this->load->library('tank_auth');
        $this->load->model('users');
    }
    function index()
    {
        //How can I get 11 from uri ?
    }
}

我在这里没有使用任何附加功能。我使用索引函数。如何获得值 11?

您可以使用

_remap()为发送到此类的任何内容运行index()函数。

class Pay extends CI_Controller
{
    function __construct()
    {
        parent::__construct();
        $this->load->library('tank_auth');
        $this->load->model('users');
    }
    function _remap($method, $params=array())
    {
        $methodToCall = method_exists($this, $method) ? $method : 'index';
        return call_user_func_array(array($this, $methodToCall), $params);
    }
    function index($val)
    {
        echo $val;
    }
}

现在,当你去www.xample.com/app/pay/11index()将被召唤。

您可以使用 codeigniter 的 URI Segment 类。它就像$this->uri->segment(n)

在您的情况下,您需要获得第 3 段。

因此,它将是——

echo $this->uri->segment(3);

http://localhost/abc/sellers/YWRtaW4=

http://localhost/abc/sellers/follow/u/1

function _remap($method, $params=array())
    {
        $method_exists = method_exists($this, $method);
        $methodToCall = $method_exists ? $method : 'index';
        $this->$methodToCall($method_exists ? $params : $method);
    }

function index($username) {
        echo $username;
}
function follow($param){
   print_r($param);
}

尝试使用此功能。

在 config/autoload 中启用 url helper.php

$autoload['helper'] = array('url');

或在所需的控制器或其方法中加载 URL 帮助程序

$this->load->helper('url');

然后在控制器中使用

$this->uri->segment(3);