如何知道在代码点火器中使用URL调用哪个控制器


How to know which controller is called using URL in codeigniter

我正在使用类似的路由

$route['Advertisement/1.0/(:any)']="v1/$1";
$route['Advertisement/1.1/(:any)']="v1_1/$1";

最终他们两个只是做同样的工作,但我必须维护他们两个,因为只是反应是一样的。

我想知道的是,我如何知道哪个控制器是使用URL调用的。如果我知道这样的URL,将相应地更改响应,这样我就不需要维护两个控制器

1.0 or 1.1

我希望你能理解我想问的问题。

提前谢谢。

根据Codeigniter的用户指南,如果您想知道命中的URL,请使用:

$uri_segments = $this->uri->uri_string();

获取URI段。

此外,您还可以使用current_url() URL助手来获取完整的URL(包括分段);要做到这一点:

// Load URL helper first (or use autoload config)
$this->load->helper('url');
// Get the current full URL
$url = current_url();

如果你想得到一个特定的URI段,可以使用:

// "n" is the segment number you wish to retrieve,
// in this case, n = 2 gets '1.0' or '1.1'
$segment = $this->uri->segment(n);

假设您的URL如下所示:example.com/Advertise/1.0/…

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

将返回1.0或1.1

如果我理解正确,您可以使用以下CI函数获得控制器名称和方法名称

$this->router->fetch_class();  // to get controller
$this->router->fetch_method(); // to get method