Jump to content
thirty bees forum
  • 0

Getting controller and id_entity by url


Question

Posted

Is it somehow possible to get the controller name and id_entity by throwing an url?

I want something like:

function getControllerAndId($url) {
	
	// Use $url somewow

	$dispatcher->getController();
}

I want to use this in a module... The idea is to recognize posted urls by customers in FrontOffice as product links and replace them.

For example: www.domain.ch/category/productXY

I want:

  • Controller=ProductController
  • id_product=352

2 answers to this question

Recommended Posts

  • 0
Posted
14 hours ago, wakabayashi said:

Is it somehow possible to get the controller name and id_entity by throwing an url?

I want something like:

function getControllerAndId($url) {
	
	// Use $url somewow

	$dispatcher->getController();
}

I want to use this in a module... The idea is to recognize posted urls by customers in FrontOffice as product links and replace them.

For example: www.domain.ch/category/productXY

I want:

  • Controller=ProductController
  • id_product=352

If you are on a bleeding edge, there is a new method Dispatcher::resolveController. We use it in tbshortcodes module for the similar purpose -- to translate internal links to shortcodes.

Note that that method has side effects (which I will hopefully fix in the future if I have time) -- it modifies global $_POST and $_GET variables. So when using that method, you should save and restore it later. I'm using it like this:

            $savePost = $_POST;
            $saveGet = $_GET;
            try {
                $_GET = [];
                $_POST = [];
                $controller = strtolower((string)Dispatcher::getInstance()->resolveController($shopId, $requestUri));
                $entityId = (int)($_GET['id_' . $controller] ?? 0);
				// $_GET and $_POST can contain other useful information extracted from request uri
            } finally {
                $_GET = $saveGet;
                $_POST = $savePost;
            }

 

  • Thanks 1

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...