How to find the route of the current page?

Is there a line of code that I can use in the controller that determines the route of the current page?

For example, I would like to find the route of a page with the SEO URL https://example.com/desktops (this should return the product/category route).

Similarly, a url, for example https://example.com/index.php?route=product/product&path=18&product_id=47 , should return the route as product/product .

+6
source share
3 answers

To be honest, the correct answer is $this->request->get['route']; .

To catch the current route, you can use $this->request->get['route']; in the catalog/controller/common/header.php file in the index() function. Header.php is part of almost any output interface (as I understand it, right now you don’t know which controller is used in http://example.com/desktops , so you need a place where you can run your code anyway).

The SEO module does not cancel this part of the request object. Also keep in mind that in the middle of generating OpenCart code, the $_GET array and the $this->request->get array are not the same. You will not catch the current route (in the "path / controller / action" format) in the $ _GET superglobal php array when the SEO module is in action, but you can catch it with any controller using the $this->request->get array, which is prepared for you by the OpenCart engine.

+12
source

This is done by querying the url_alias table for a particular keyword to give you one of the four identifiers that come with the standard setting ( product_id , category_id , information_id and manufacturer_id ) along with this id value, separated by = .

In the desktop example, this will be something like category_id=20 . From there you need to work out the route

You can find the exact way that OpenCart does this in the file /catalog/controller/common/seo_url.php

0
source

EDIT:

There is $this->request->get['route'] in the controller.

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'; } 
0
source

Source: https://habr.com/ru/post/948142/


All Articles