[英]Get uri segment in index function of controller - Codeigniter
I have a url 我有一个网址
www.xample.com/app/pay/11 www.xample.com/app/pay/11
where pay is my controller. 工资是我的控制人。 The controller is defined as
控制器定义为
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 ?
}
}
Im not using any additional function here.Im using an index function.How could I get the value 11? 我在这里没有使用任何附加功能,我在使用索引功能,我怎么能得到值11?
You could use _remap()
to run the index()
function for anything sent to this class. 您可以使用
_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;
}
}
Now, when you go to www.xample.com/app/pay/11
, index()
will be called. 现在,当您访问
www.xample.com/app/pay/11
,将调用index()
。
You can use the URI Segment class of codeigniter.. it like $this->uri->segment(n)
您可以使用codeigniter的URI Segment类。它像
$this->uri->segment(n)
In your case, you need to get the 3rd segment. 在您的情况下,您需要获得第三段。
Hence, it will be- 因此,它将是-
echo $this->uri->segment(3);
http://localhost/abc/sellers/YWRtaW4= http:// localhost / abc / sellers / YWRtaW4 =
http://localhost/abc/sellers/follow/u/1 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);
}
Try to use this function.. 尝试使用此功能。
enable url helper in config/autoload.php 在config / autoload.php中启用url helper
$autoload['helper'] = array('url');
or load url helper in desired controller or its method 或在所需的控制器或其方法中加载网址帮助器
$this->load->helper('url');
and then use below in controller 然后在控制器中使用下面
$this->uri->segment(3);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.