-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Fix matching prefix for api routes #1
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
Conversation
Looks good. What was it causing 404s on exactly? Can you give me some sample code? Also it looks like you're using spaces, any chance you can swap them for tabs. Cheers! |
Sorry for the spaces!. Using:
I was getting a NotFoundException, if I copy and pasted the same code below and change the version to v2, it worked. It was awkward. Then if I was using the prefix 'api' and hitting '/apianything/users' it was working also. So I figured it was the matcher for prefixes, and the bug was exactly in this line:
And the trim was made on the prefix that I've set on the route so it was 'api'. Hope it was clear :) |
Okay it's a bug. But this still isn't going to fix it because it won't match a prefix that has a slash in it. Route::api(['version' => 'v1', 'prefix' => 'foo/bar'], function()
{
// Any routes in here would not be found.
}); Something like this should do the trick I think: /**
* Matches prefix if is set in route group
*
* @param $request
* @return bool
*/
protected function matchPrefix($request)
{
if ( ! $prefix = $this->option('prefix'))
{
return false;
}
$prefix = $this->filterAndExplode($this->option('prefix'));
$path = $this->filterAndExplode($request->getPathInfo());
return $prefix == array_slice($path, 0, count($prefix));
}
/**
* Explode array on slash and remove empty values.
*
* @param array $array
* @return array
*/
protected function filterAndExplode($array)
{
return array_filter(explode('/', $array));
} |
Totally right! I forgot the slashes. I've updated the code with yours and the tests to match also prefixes with slashes Excellent thanks for everything! |
Merged this in via command line to squash some commits. Cheers! (I think you probably should've squashed them, ah well.) |
Thanks mate! Sorry I'm kind of new with best practices on PR, I'll doit next time. |
Instead of using the existing Laravel router we will use a new instance. This should prevent the APIs internal dispatcher from overriding the current route on the actual Laravel router instance. There does not seem to be any need for the router instance that is injected into the adapter to be the actual Laravel router, as the adapter just needs an instance to dispatch the API-only routes.
Merging upstream to master
…eptions cannot be null update composer json fix header check fix tests, catch session not found exception update applicationStubs add application stubs for laravel 9 , update version check fix get check version fix passing null to trim as dingo#1 argument fix deprecation for getClass preg_replace(): Passing null to parameter dingo#2 ($replacement) of type array|string is deprecated in C:\laragon\www\api\src\Http\Validation\Domain.php on line 69 match signature bump laravel, phpdocumentor
Hey mate!, I've fixed the prefix it was matching anything at the start of the string and it was causing 404s on the api routes. Also made a test on it.
I hope it helps, let me know! ;)