Reputation: 2166
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
).
Similarly, url such as https://example.com/index.php?route=product/product&path=18&product_id=47
should return route as product/product
.
Upvotes: 11
Views: 37728
Reputation: 1693
Right answer is $this->request->get['route'];
.
In order to catch the current route, you can use $this->request->get['route'];
in the index()
function of the catalog/controller/common/header.php
file. The header.php
file is a part of practically any frontend output (as I understand, right now you are not sure what controller is used in the http://example.com/desktops
URL, so you need right place where you can run your code in any case).
The SEO module does not unset this part of the request object. Also, keep in mind, in the middle of OpenCart code generation the $_GET
array and the $this->request->get
array are not the same things. You won't catch current route (in "path/controller/action" format) in the $_GET
superglobal PHP's array when SEO module is in action, but you can catch it in any controller by using a $this->request->get
array which is prepared for you by OpenCart engine.
Upvotes: 16
Reputation: 3618
EDIT:
In controller we have $this->request->get['route']
.
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';
}
Upvotes: 1
Reputation: 15151
This is done by querying the url_alias
table for the specific keyword to give you one of the four ID's that come with the standard setup (product_id
, category_id
, information_id
and manufacturer_id
) along with it's id value separated by an =
.
In your desktops example, it would be something like category_id=20
. From there, you need to work out the route
You can find the exact way OpenCart does this in the file /catalog/controller/common/seo_url.php
Upvotes: 1