Skip to content

[8.x] Fix array keys from cached routes#42078

Merged
taylorotwell merged 1 commit intolaravel:8.xfrom
glamorous:fix-route-caching
Apr 25, 2022
Merged

[8.x] Fix array keys from cached routes#42078
taylorotwell merged 1 commit intolaravel:8.xfrom
glamorous:fix-route-caching

Conversation

@glamorous
Copy link
Copy Markdown
Contributor

Introduced by #35714.
Removed the slash to match the same array key as in
src/Illuminate/Routing/RouteCollection.php:60

We noticed a different result when we used route caching for in this code piece:

$url = parse_url($requestUri, PHP_URL_PATH);

 if (!is_string($url)) {
        return true;
 }

$routeKey = '{tenant}.' . trim(config('app.domain')) . trim($url, '/');

$routes = Route::getRoutes()->get($requestMethod);

return array_key_exists($routeKey, $routes);

Only routes with a specified domain where affected and a the result array of "->get('GET')" would not have the same array keys. Check next example with uri "test" and domain "{tenant}.laravel.test", this would give these two different results if you use cached router or not:

"{tenant}.laravel.test/test" => cached
"{tenant}.laravel.testtest" => not cached

The check for the existing of a domain is not necessary because the "->getDomain" function can return null as well and wil result in empty string (just like it behaves in the non compiled RouteCollection).

If approved & merged, I can cherry-pick it for 9.x release as well.

@driesvints
Copy link
Copy Markdown
Member

I don't think this is valid? Seems like existing tests are breaking because of this PR.

@taylorotwell
Copy link
Copy Markdown
Member

taylorotwell commented Apr 21, 2022

@glamorous please provide a bullet point list of steps to take in a fresh Laravel application to recreate the bug. Also please mark this PR as ready for review when you have done so.

@taylorotwell taylorotwell marked this pull request as draft April 21, 2022 14:44
@glamorous
Copy link
Copy Markdown
Contributor Author

  1. Create new Laravel application (8.x or 9.x)
  2. Create a route (important to set a "domain" on the route)
Route::middleware(['web'])
    ->domain('{tenant}.' . 'laravel.test')
    ->where(['tenant' => '^((?!(admin|api|webhooks)).)*$'])
    ->get('/test', function () {
       return view('welcome');
    });
  1. Open tinker-session:

array_keys(Route::getRoutes()->get('GET'));

  1. Result:
[
     "sanctum/csrf-cookie",
     "_ignition/health-check",
     "api/user",
     "/",
     "{tenant}.laravel.testtest",
   ]
  1. Close tinker session
  2. Run php artisan route:cache in console
  3. Redo step 3 (tinker session)
  4. Result:
[
     "sanctum/csrf-cookie",
     "_ignition/health-check",
     "api/user",
     "/",
     "{tenant}.laravel.test/test",
   ]
  1. See the difference in array keys from both results.

@glamorous glamorous marked this pull request as ready for review April 21, 2022 15:09
@GrahamCampbell GrahamCampbell changed the title fix array keys from cached routes [8.x] Fix array keys from cached routes Apr 21, 2022
@driesvints
Copy link
Copy Markdown
Member

It indeed seems that this is the correct fix. We'll need to adjust the tests and also version the routes file anew to make sure existing cached route files aren't conflicting with this change.

@glamorous
Copy link
Copy Markdown
Contributor Author

I'll try to find some time this weekend to check which tested needs an update

@taylorotwell
Copy link
Copy Markdown
Member

Can we get a test on this?

Introduced by #35714.
Removed the slash to match the same array key as in
src/Illuminate/Routing/RouteCollection.php:60
@glamorous
Copy link
Copy Markdown
Contributor Author

There was just a test that needed the fix as well to match the changed code. If merged, I can cherry-pick for 9.x

@driesvints
Copy link
Copy Markdown
Member

@taylorotwell there were tests for this, they've now been changed to the new behavior 👍

@taylorotwell taylorotwell merged commit d363435 into laravel:8.x Apr 25, 2022
@glamorous glamorous deleted the fix-route-caching branch April 25, 2022 14:34
taylorotwell pushed a commit that referenced this pull request Apr 25, 2022
Introduced by #35714.
Removed the slash to match the same array key as in
src/Illuminate/Routing/RouteCollection.php:60
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.

3 participants