php 如何找到当前页面的路径?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17321029/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to find route of a current page?
提问by user1448031
Is there a line of code that I can use in the controller that determines the route of a current page?
我可以在控制器中使用一行代码来确定当前页面的路由吗?
For example, I would like to find the route of the page with SEO url https://example.com/desktops(this should return the route product/category).
例如,我想找到带有 SEO url 的页面的路由https://example.com/desktops(这应该返回 route product/category)。
Similarly, url such as https://example.com/index.php?route=product/product&path=18&product_id=47should return route as product/product.
同样,诸如 url 之类的 urlhttps://example.com/index.php?route=product/product&path=18&product_id=47应将路由返回为product/product.
回答by Sergey Ronin
To be honest, the right answer is $this->request->get['route'];.
老实说,正确答案是$this->request->get['route'];。
In order to catch the current route, you can use $this->request->get['route'];in the catalog/controller/common/header.phpfile in the index()function. Header.phpis a part of practically any frontend output (as I understand, right now you are not sure what controller is used in http://example.com/desktopsso you need right place where you can run your code in any case).
为了捕获当前路由,您可以在函数$this->request->get['route'];中的catalog/controller/common/header.php文件中使用index()。Header.php几乎是任何前端输出的一部分(据我所知,现在您不确定使用哪个控制器,http://example.com/desktops因此您需要在任何情况下都可以运行代码的正确位置)。
The SEO module does not unset this part of the request object. Also, keep in mind, in the middle of OpenCart code generation the $_GETarray and the $this->request->getarray is not the same things. You won't catch current route (in "path/controller/action" format) in $_GET superglobal php array when SEO module is in action, but you can catch it with any controller by using $this->request->getarray which is prepared for you by OpenCart engine.
SEO 模块不会取消设置请求对象的这部分。另外,请记住,在 OpenCart 代码生成过程中,$_GET数组和$this->request->get数组不是一回事。当 SEO 模块运行时,您不会在 $_GET 超全局 php 数组中捕获当前路由(以“路径/控制器/操作”格式),但您可以使用任何控制器捕获它,使用$this->request->getOpenCart 引擎为您准备的数组.
回答by Jay Gilford
This is done by querying the url_aliastable for the specific keyword to give you one of the four ID's that come with the standard setup (product_id, category_id, information_idand manufacturer_id) along with it's id value separated by an =.
这是通过查询url_alias特定关键字的表来完成的,以便为您提供标准设置(product_id、category_id、information_id和manufacturer_id)附带的四个 ID 之一以及由=.分隔的 id 值。
In your desktops example, it would be something like category_id=20. From there, you need to work out the route
在您的桌面示例中,它类似于category_id=20. 从那里,您需要制定路线
You can find the exact way OpenCart does this in the file /catalog/controller/common/seo_url.php
您可以在文件中找到 OpenCart 执行此操作的确切方式 /catalog/controller/common/seo_url.php
回答by Nikita U.
EDIT:
编辑:
In controller we have $this->request->get['route'].
在控制器中,我们有$this->request->get['route'].
seo_url.php:
seo_url.php:
if (isset($this->request->get['product_id'])) {
$this->request->get['route'] = 'product/product';
} elseif (isset($this->request->get['path'])) {
$this->request->get['route'] = 'product/category';
} elseif (isset($this->request->get['manufacturer_id'])) {
$this->request->get['route'] = 'product/manufacturer/info';
} elseif (isset($this->request->get['information_id'])) {
$this->request->get['route'] = 'information/information';
}

