CodeIgniter -获取当前页面路由

q3qa4bjr  于 2022-12-07  发布在  其他
关注(0)|答案(7)|浏览(194)

我正在创建一个页面访问者模块。目标是了解访问最多的用户,以及访问最多的模块。
我想获取当前页面的页面路由。
下面是我想做的事情:
1.我有一个模块叫库存模块。
1.此模块有一个路由'merchandiser/report/inventory'。(在routes.php中,它是$route['merchandiser/report/inventory'])
这是我想要的路线。
我试过:

<?php echo current_url(); ?>

但它返回的结果是这样的。

192.168.3.3/portal/merchandiser/report/inventory

192.168.3.3/portal/是我的基本网址'
现在我的问题是:
如何删除192.168.3.3/portal/而只得到merchandiser/report/inventory
希望你能指引我或者带我去哪里找它。
谢谢您的支持。

dfty9e19

dfty9e191#

希望这对您有所帮助:
使用url助手的

uri_string();

如果您URL是:

http://some-site.com/blog/comments/123

该函数将返回:

blog/comments/123

有关详细信息,请访问:https://www.codeigniter.com/user_guide/helpers/url_helper.html#uri_string
或者您可以使用uri,类似于:

echo $this->uri->segment(1).'/'.$this->uri->segment(2).'/'.$this->uri->segment(3); 

/*produces controller/method/parameter structure

https://www.codeigniter.com/user_guide/libraries/uri.html

lokaqttq

lokaqttq2#

你可以在codeigniter中使用这个。

$this->uri->segment(1); // controller
$this->uri->segment(2); // action
$this->uri->segment(3); // 1stsegment
$this->uri->segment(4); // 2ndsegment

它将返回当前URL的段。

ht4b089n

ht4b089n3#

这个作品很有魅力:

$controller = $this->router->fetch_class();
    $method = $this->router->fetch_method();
    echo $this->uri->uri_string();

您可以打印出这些值或在身份验证后使用redirect(“$controller/$method”)进行重定向;

vd8tlhqk

vd8tlhqk4#

在codeigniter 4中,您可以设置路由名称并使用来获取路由名称

$router = \CodeIgniter\Config\Services::router();
$current_route = $router->getMatchedRouteOptions()['as'];

并得到控制器和方法

$router->controllerName()
$router->methodName();
ix0qys7i

ix0qys7i5#

我认为代码点火器urlhelper应该做的工作与:

uri_string(
    current_url()
)
oxf4rvwz

oxf4rvwz6#

使用此

echo base_url(uri_string());
dly7yett

dly7yett7#

我用它来获取当前页面:

if (current(array_reverse(explode("/",current_url()))) == 'contato') { }

相关问题