# How can I use a URL to read out the controller, action and any other parameters?

**URL:** <https://discourse.cakephp.org/t/how-can-i-use-a-url-to-read-out-the-controller-action-and-any-other-parameters/11349>\
**Category:** Need Help\
**Tags:** help\
**Created:** [July 14, 2023, 6:59pm UTC](https://discourse.cakephp.org/t/how-can-i-use-a-url-to-read-out-the-controller-action-and-any-other-parameters/11349 "2023-07-14T18:59:11Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ProDaniBest](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/prodanibest/32/2521_2.png) [@ProDaniBest](https://discourse.cakephp.org/u/ProDaniBest)\
**Post date:** [July 14, 2023, 6:59pm UTC](https://discourse.cakephp.org/t/how-can-i-use-a-url-to-read-out-the-controller-action-and-any-other-parameters/11349/1 "2023-07-14T18:59:11Z")

</div>

Hi,

I’m currently building a helper class for my menu. It is important that I know whether the page I clicked on in the menu is the right one. Because if so, I can then add a css class.

Example:  
I have a menu pointing to [http://localhost/shop/view/1](http://localhost/shop/view/1). Now I would like to read out the controller, the action and, if necessary, all other parameters. I also have some zsm. tinkered, but that doesn’t work. Can someone help me there? 🙂

```auto
...
use Cake\Http\ServerRequest;
use Cake\View\Helper;
use Cake\Routing\Router;
....

public function isCurentActive( $url ) {
		$request = new ServerRequest( array('url' => $url) );
		$route = Router::parseRequest( $request );
		$controller = $route['controller'];
		$action = $route['action'];
		$request = Router::getRequest();
		$actualController = $request->getParam( 'controller' );
		$actualAction = $request->getParam( 'action' );
		$actualPassedArgs = $request->getParam( 'pass' );
		return ($actualController === $controller) && ($actualAction === $action);
	}

```

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [July 14, 2023, 7:52pm UTC](https://discourse.cakephp.org/t/how-can-i-use-a-url-to-read-out-the-controller-action-and-any-other-parameters/11349/2 "2023-07-14T19:52:37Z")

</div>

What is not working about this? What sort of data are you passing into this for the `$url` (string or array), and if a string, why?

---

<div class="post-metadata">

**Author:** ![ProDaniBest](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/prodanibest/32/2521_2.png) [@ProDaniBest](https://discourse.cakephp.org/u/ProDaniBest)\
**Post date:** [July 15, 2023, 6:04am UTC](https://discourse.cakephp.org/t/how-can-i-use-a-url-to-read-out-the-controller-action-and-any-other-parameters/11349/3 "2023-07-15T06:04:06Z")

</div>

Hello @Zuluru 🙂

The URL parameter is a **string**. And looks something like this: “**[http://localhost/shop/view/1](http://localhost/shop/view/1)**” The URLs are stored in a database and are output as a `<ul><li>` list in a loop. This is how the menu is created.

Now I have to check if the link I saved in the database matches the currently visited page.

Currently I get the following error message:

# Missing Route

…  
See the screenshot

 ![erro_cakephp](https://canada1.discourse-cdn.com/flex029/uploads/cakephp/original/2X/f/fd6cc5094395f67d487a001ec923ed495ab15d41.png)

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [July 15, 2023, 6:53pm UTC](https://discourse.cakephp.org/t/how-can-i-use-a-url-to-read-out-the-controller-action-and-any-other-parameters/11349/4 "2023-07-15T18:53:48Z")

</div>

There’s a “/” at the beginning of the URL it’s trying to match: “/http…”. I’m not familiar with what `Router::parseRequest` does, but maybe it’s expecting just a URL fragment, not the entire thing with domain name included?

---

<div class="post-metadata">

**Author:** ![ADmad](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/admad/32/20_2.png) [@ADmad](https://discourse.cakephp.org/u/ADmad)\
**Post date:** [July 16, 2023, 2:53pm UTC](https://discourse.cakephp.org/t/how-can-i-use-a-url-to-read-out-the-controller-action-and-any-other-parameters/11349/5 "2023-07-16T14:53:18Z")

</div>

```auto
// URL should be without protocol and domain name
$route = Router::getRouteCollection()->parse('/shop/view/1');

```

---

<div class="post-metadata">

**Author:** ![ProDaniBest](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/prodanibest/32/2521_2.png) [@ProDaniBest](https://discourse.cakephp.org/u/ProDaniBest)\
**Post date:** [July 16, 2023, 6:30pm UTC](https://discourse.cakephp.org/t/how-can-i-use-a-url-to-read-out-the-controller-action-and-any-other-parameters/11349/6 "2023-07-16T18:30:41Z")

</div>

Thank you 🙂 I managed it with the help of you. Thanks very much 🙂
