Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accessing and modifying the current route #13661

Closed
1 task done
smarek opened this issue Sep 20, 2019 · 5 comments
Closed
1 task done

Accessing and modifying the current route #13661

smarek opened this issue Sep 20, 2019 · 5 comments
Labels
Milestone

Comments

@smarek
Copy link

smarek commented Sep 20, 2019

For CakePHP version 3.7.9

  • Enhancement

There is, afaik, currently no easy way to access current route and array of attributes, that matched the route

What is known (accessing current route/url build params):

  • Cake\Http\ServerRequest method getRequestTarget() returns only string representation (including query and without hash url params)
  • Cake\Routing\Router::reverseToArray($this->request) will return array of params to build the url, however including params not contained in current url (such as _csrfToken, isAjax)
  • Cake\View\Helper\UrlHelper method build() used like $this->Url->build(Router::reverseToArray($this->request)) will return url as string, but containing _csrfToken as get query param (see point above)
  • Cake\Routing\Router::parseRequest($this->request) will return relevant array of params to build array, but including _matchedRoute

What is known (building new url based on current one, given that ':lang' is recognized route segment)

  • $this->Url->build(['lang'=>'fr'] + Router::reverseToArray($this->request)) will generate url that contains additional data (eg. _csrfToken)
  • $this->Url->build(['lang'=>'fr'] + Router::parseRequest($this->request)) will generate url that contains additional data (eg. _matchedRoute)

Since there is always some extra data in the returned, developer cannot be sure, that even manually filtering values that are apparent, will filter out all the extra data, that could appear (ie. it is not known if the data in reverseToArray, parseRequest and others will not contain more unnecessary attributes under different circumstances)

So what I'm seeking is documentation update or API update, that would provide clear way of

  1. Retrieving only relevant array of data needed to build current URL (matching current route)
  2. Modifying current route param(s) and re-generating (reverse routing url build) url (eg. using Url->build, Html->link or Html->url)
@markstory markstory added this to the 3.8.5 milestone Sep 21, 2019
@markstory
Copy link
Member

Have you tried using the Uri object, it can return the path in isolation.

// Get the path (without subdirectory)
$request->getUri()->getPath();

Which can then be used with any of the helpers to get paths to the current URL.

@saeideng
Copy link
Member

if you use _name in routes , its possible to detect current route

@ADmad
Copy link
Member

ADmad commented Sep 21, 2019

$this->Url->build(Router::reverseToArray($this->request)) will return url as string, but containing _csrfToken as get query param (see point above)

This seems like a bug / oversight. Router::reverseToArray() should unset _csrfToken key too like it unsets _Token.

@ADmad
Copy link
Member

ADmad commented Sep 22, 2019

Closing as Router::reverseToArray() cleans out _csrfToken and isAjax keys too now.

The main culprit here is the router feature which turns any extra key in URL array into query string param. This has already been changed in 4.x.

@ADmad ADmad closed this as completed Sep 22, 2019
@smarek
Copy link
Author

smarek commented Sep 23, 2019

Thank you all :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants