Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Routing] Added trim flag to Route::setPath #18702

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

Closed
wants to merge 1 commit into from

Conversation

ragboyjr
Copy link
Contributor

@ragboyjr ragboyjr commented May 3, 2016

Q A
Branch? master
Bug fix? no
New feature? yes
BC breaks? yes
Deprecations? no
Tests pass? yes
Fixed tickets none
License MIT
Doc PR N/A

silexphp/Silex#149

Added a trim flag to the Route setPath. This replaces #18701

Signed-off-by: RJ Garcia [email protected]

@ragboyjr ragboyjr force-pushed the route-path-trim-flag branch 2 times, most recently from 558afe6 to f8d1b6e Compare May 3, 2016 16:26
Added a trim flag to Route::setPath in order to allow paths
to be set as is without any trimming to sanitize the input

Signed-off-by: RJ Garcia <[email protected]>
@ragboyjr ragboyjr force-pushed the route-path-trim-flag branch from f8d1b6e to e1fc962 Compare May 3, 2016 16:27
@ragboyjr
Copy link
Contributor Author

ragboyjr commented May 3, 2016

Hmm, something in the DependencyInjection component is making travis fail on hhvm.

@ragboyjr
Copy link
Contributor Author

ragboyjr commented May 3, 2016

@Ener-Getick, @stof, @GromNaN, Oh, you know, I just though of another solution that could actually work for everyone.

What if we added a new protected method in the Route like this

public function setPath($pattern)
{
    // A pattern must start with a slash and must not have multiple slashes at the beginning because the
    // generated path for this route would be confused with a network path, e.g. '//domain.com/path'.
    return $this->doSetPath('/'.ltrim(trim($pattern), '/'));
}

protected function doSetPath($pattern) 
{
    $this->path = $pattern;
    $this->compiled = null;

    return $this;
}

or, instead of protected, we could always make doSetPath public and change the name to something like setPathRaw or whatever.

Either way, this wouldn't break BC, and is a cleaner solution that prevents "monkey patching"

@xabbuh
Copy link
Member

xabbuh commented May 3, 2016

Can you describe your use case where you would like to have trimming disabled?

@ragboyjr
Copy link
Contributor Author

ragboyjr commented May 3, 2016

@xabbuh silexphp/Silex#149

Basically, there is an issue with mounted/prefixed routes.

where if the prefix was /user, you'd never be able to add a route at /user, but only /user because an empty route '' is converted to '/'.Since silex is mostly used for RESTful API's, this is troublesome. Being able to disable the trimming on prefixed routes would solve this issue.

{
// A pattern must start with a slash and must not have multiple slashes at the beginning because the
// generated path for this route would be confused with a network path, e.g. '//domain.com/path'.
$this->path = '/'.ltrim(trim($pattern), '/');
$this->path = $trim ? '/'.ltrim(trim($pattern), '/') : $pattern;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the double slash removal must be kept as described in the comment above

Copy link
Contributor Author

@ragboyjr ragboyjr May 3, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Tobion Well, what would you think about doing this instead: #18702 (comment)

@GuilhemN
Copy link
Contributor

GuilhemN commented May 3, 2016

It took me some time to understand the issue so in case it helps here is an illustration with a routing file:

// routing.yml
sub_routing:
   resource: sub_routing.yml
   prefix: /foo
// sub_routing.yml
my_route:
    path: ''

// you would expect to be able to access /foo here 
// but you can only access /foo/ as paths are always
// prefixed with a /

@ragboyjr
Copy link
Contributor Author

ragboyjr commented May 3, 2016

@Ener-Getick, that is precisely it. I wasn't accustomed to the Syfmony configuration for this because I was referring to Silex, but, yes it's the same exact issue.

@GuilhemN
Copy link
Contributor

GuilhemN commented May 3, 2016

Another solution could be to detect empty paths and to not format them.
This has the advantage to fix the initial issue in symfony too but could be considered as a bc break.

@ragboyjr
Copy link
Contributor Author

ragboyjr commented May 3, 2016

@Ener-Getick, that would work for me as well, but that would be also be a BC break, unless we considered this a bug in the way it handles empty urls.

And it could potentially break someone's code if they were expecting that '' are silently converted to '/'

But if that's not a documented feature, then we might be able to get away with it.

@javiereguiluz
Copy link
Member

This seems related to #12141 too.

@fabpot
Copy link
Member

fabpot commented Jul 6, 2017

I'm closing here as what @GuilhemN describes is exactly how this should work. When mounting a resource, like on a filesystem, the prefix is a directory.

@fabpot fabpot closed this Jul 6, 2017
@ragboyjr
Copy link
Contributor Author

ragboyjr commented Jul 6, 2017

@fabpot sounds good, thanks.

@ragboyjr ragboyjr deleted the route-path-trim-flag branch July 6, 2017 15:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants