laravel-localization icon indicating copy to clipboard operation
laravel-localization copied to clipboard

Route caching doesn't work in Laravel 11

Open vincentsch opened this issue 11 months ago • 9 comments

Describe the bug Route caching doesn't work in Laravel 11. The package's caching documentation relies on RouteServiceProvider which has been removed in Laravel 11.

To Reproduce

  1. Create new Laravel 11 project
  2. Install mcamara/laravel-localization
  3. Follow caching setup guide at https://github.com/mcamara/laravel-localization/blob/master/CACHING.md
  4. Get error when running artisan route:trans:cache because RouteServiceProvider doesn't exist
  5. Without those modifications get 404 errors on localized routes after caching routes

Expected behavior Route caching should work with Laravel 11's new routing system

More info:

  • Laravel Version: 11.x
  • Laravel-localization Version: 1.8.2
  • Middleware: localize, localizationRedirect, localeCookieRedirect, localeViewPath
  • Config:
'supportedLocales' => [
    'en' => ['name' => 'English', 'script' => 'Latn', 'native' => 'English', 'regional' => 'en_GB'],
    'de' => ['name' => 'German', 'script' => 'Latn', 'native' => 'Deutsch', 'regional' => 'de_DE']
],
'useAcceptLanguageHeader' => false,
'hideDefaultLocaleInURL' => true

Additional context Current caching documentation at https://github.com/mcamara/laravel-localization/blob/master/CACHING.md is incompatible with Laravel 11.

vincentsch avatar Jan 04 '25 06:01 vincentsch

Thank you for reporting this bug. Unfortunately, I’m not aware of a simple workaround for version 11 at the moment. However, we are actively working on version 3 of this package, which will eliminate the need for custom caching and fully leverage Laravel’s built-in cache. This update will resolve the issue once it’s ready.

niels-numbers avatar Jan 04 '25 12:01 niels-numbers

@iwasherefirst2 Excellent news! 👍

Laravel 10 has reached End of Life today, when can we expect version 3 of Laravel Localization ?

And would version 3 also allow the use of Octane (see #780 & #828) ?

Thanks & Good luck for version 3! 💪

jbelien avatar Feb 04 '25 16:02 jbelien

Hopefully version 3 will support older versions, such as laravel8, but due to historical reasons, I haven't been able to upgrade to laravel10 and 11 for the time being

szwss avatar Feb 11 '25 15:02 szwss

@jbelien The translated routes are my biggest concern. I need to find a good solution that minimizes breaking changes. I expect version 3.x to be released within the next 2–3 months at the latest.

I believe our changes will make it compatible with Octane, but that's not a goal for this release. It would be great if someone could test it once 3.x is ready and confirm

@szwss I’d like to ensure compatibility with Laravel 9, but I can't make any guarantees at this point.

niels-numbers avatar Feb 11 '25 21:02 niels-numbers

I can't get route caching to work with Laravel 12 either - are there any plans for updates?

a-smily avatar Apr 02 '25 18:04 a-smily

Heads up - the new Laravel Forge includes php artisan optimize by default in the deploy script now.

This means that if you're using this package and you're deploying on the new Forge, all your routes that use this package will 404.

For now until a fix is available, you will need to remove php artisan optimize from your deploy script and run php artisan optimize:clear to remove any existing route cache.

voidgraphics avatar Oct 10 '25 10:10 voidgraphics

There will be some news soon to fix this issue.

niels-numbers avatar Oct 10 '25 10:10 niels-numbers

Having the same issue. Did anybody find a workaround for this?

simplexx avatar Oct 15 '25 07:10 simplexx

@niels-numbers caching in Laravel 11 works, docs for this were PR'd in #902. I improved some of them in #950 in case you want to proofread.

I agree the current caching experience isn't optimal, because commands like php artisan optimize will still call php artisan route:cache instead of php artisan route:trans:cache. I guess we could override this behaviour by using the optimizes method on the Illuminate\Support\ServiceProvider class for this. I might look into this and create a PR.

use Illuminate\Support\ServiceProvider;
use Mcamara\LaravelLocalization\Traits\LoadsTranslatedCachedRoutes;

class AppServiceProvider extends ServiceProvider
{
    use LoadsTranslatedCachedRoutes;

    public function boot(): void
    {
        RouteServiceProvider::loadCachedRoutesUsing(fn () => $this->loadCachedRoutes());

        if ($this->app->runningInConsole()) {
            $this->optimizes(
                optimize: 'route:trans:cache',
                clear: 'route:trans:clear',
                key: 'routes'
            );
        }
    }
}

jordyvanderhaegen avatar Oct 20 '25 21:10 jordyvanderhaegen