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

Unexpected behavior for request->getRequestTarget() #11977

Closed
1 of 3 tasks
BassemN opened this issue Apr 21, 2018 · 8 comments
Closed
1 of 3 tasks

Unexpected behavior for request->getRequestTarget() #11977

BassemN opened this issue Apr 21, 2018 · 8 comments
Milestone

Comments

@BassemN
Copy link

BassemN commented Apr 21, 2018

This is a (multiple allowed):

  • bug

  • enhancement

  • feature-discussion (RFC)

  • CakePHP Version: 3.6.1.

  • Platform and Target: Apache/2.4.27, MySQL 5.6.37

What you did

I upgraded my App from 3.5 to 3.6. and my app is located into a sub directory called myapp so the full URL is http://development.local/myapp/

What happened

I got a warning error says Accessing 'here' as a property will be removed in 4.0.0. Use request->getRequestTarget() instead. - /www/myapp/src/Template/Layout/front.ctp, Line 45 so I used getRequestTarget() instead of here

What you expected to happen

I expected that request->getRequestTarget() will return the same reult from request->here but this was not the case. As I told you above that my app is localed at http://development.local/myapp/
in 3.5 & 3.6, request->here returns /myapp/ but on 3.6 request->getRequestTarget() returns / which is a different result and it should return /myapp/

Thanks

@markstory markstory added this to the 3.6.2 milestone Apr 21, 2018
@markstory
Copy link
Member

Have you triedgetPath()? It was added in 3.6.1 to help with updating the url property, and may also fit your needs.

@markstory markstory added the http label Apr 21, 2018
@BassemN
Copy link
Author

BassemN commented Apr 21, 2018

getPath() gives the same result for getRequestTarget() / instaed of /myapp/

@markstory
Copy link
Member

So it does. My mistake.

When used with Router::url() is used with getRequestTarget() you should get the same value as here.

I don't use here at all personally. In what scenarios do you find the here property useful? We could add more methods I just worry about adding more methods for less frequently used operations.

@BassemN
Copy link
Author

BassemN commented Apr 21, 2018

I use here in my layout to detect if I'm on the homepage or not using the following condition:

if($this->request->here == $this->request->getAttribute('webroot')) {
   // I'm on homepage
}

maybe there is a better method but this is how I did it before :)

@dereuromark
Copy link
Member

Shouldnt you be using

->getRequestTarget() === '/'

instead?

@BassemN
Copy link
Author

BassemN commented Apr 22, 2018

@dereuromark Yes, your suggestion will work well but here and getRequestTarget() are not working in the same manner if your app in a subdirectory so the deprecated warning message is not accurate when asking me to use getRequestTarget() instead.

ADmad added a commit that referenced this issue Apr 22, 2018
@markstory
Copy link
Member

@BassemN For that usage, I think you would want getPath(). Both here and getRequestTarget() in that context would run into issues if a query string is present.

I'm reluctant to add a method to completely reproduce here as applications should generally not need to be aware that they exist in a sub-directory outside of configuration. Ideally Router and other abstractions hide that 'detail'.

@BassemN
Copy link
Author

BassemN commented Apr 23, 2018

Thanks allot :)

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

No branches or pull requests

4 participants