Skip to content

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

Closed
wants to merge 3 commits into from

Conversation

joecohens
Copy link
Contributor

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! ;)

@jasonlewis
Copy link
Contributor

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!

@joecohens
Copy link
Contributor Author

Sorry for the spaces!.

Using:

Route::api(['version' => 'v1', 'prefix' => 'api', 'protected' => true], function()
{
    Route::post('token', ['protected' => false, function()
    {
          $payload = Input::only('grant_type', 'client_id', 'client_secret', 'username', 'password', 'scope');

          return API::issueToken($payload);
    }]);

    Route::get('users', ['protected' => true, function()
    {
          $user = User::all();

          return API::user();
    }]);
});

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:

starts_with($request->getPathInfo(), trim($this->option('prefix'), '/'))

$request->getPathInfo() // Was /api/users

And the trim was made on the prefix that I've set on the route so it was 'api'.

Hope it was clear :)

@jasonlewis
Copy link
Contributor

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));
}

@joecohens
Copy link
Contributor Author

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!

@jasonlewis
Copy link
Contributor

Merged this in via command line to squash some commits.

e13f825

Cheers!

(I think you probably should've squashed them, ah well.)

@jasonlewis jasonlewis closed this Apr 12, 2014
@joecohens joecohens deleted the bug/prefix-matching branch April 14, 2014 05:22
@joecohens joecohens restored the bug/prefix-matching branch April 14, 2014 05:25
@joecohens joecohens deleted the bug/prefix-matching branch April 14, 2014 05:30
@joecohens
Copy link
Contributor Author

Thanks mate! Sorry I'm kind of new with best practices on PR, I'll doit next time.

@xLink xLink mentioned this pull request Dec 3, 2014
@kabelo38 kabelo38 mentioned this pull request May 12, 2015
wayne5w referenced this pull request Mar 17, 2016
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.
@achegedus achegedus mentioned this pull request Mar 21, 2016
@banandam banandam mentioned this pull request Apr 21, 2016
specialtactics added a commit that referenced this pull request Jan 25, 2019
Merging upstream to master
specialtactics pushed a commit that referenced this pull request Feb 17, 2019
Leonardo-Atalla added a commit to EpsilonTelecommunications/api that referenced this pull request Sep 13, 2022
…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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants