Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 0 additions & 73 deletions CACHING.md

This file was deleted.

66 changes: 39 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -496,52 +496,64 @@ Be sure to pass the locale and the attributes as parameters to the closure. You

## Caching routes

To cache your routes, use:
> [!CAUTION]
> By default, this package is not compatible with Laravel’s route caching.
> Running commands such as `php artisan route:cache` or `php artisan optimize` will cause localized routes to return 404 errors.

``` bash
php artisan route:trans:cache
```

... instead of the normal `route:cache` command. Using `artisan route:cache` will **not** work correctly!
To enable route caching for your localized routes, you may use the `LoadsTranslatedCachedRoutes` trait provided by this package.
Depending on your Laravel version, you will need to apply the trait differently:

For the route caching solution to work, it is required to make a minor adjustment to your application route provision.

**before laravel 11**

In your App's `RouteServiceProvider`, use the `LoadsTranslatedCachedRoutes` trait:
**Before Laravel 11**
If your application includes a `RouteServiceProvider`, add the `LoadsTranslatedCachedRoutes` trait to it:

```php
<?php
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
use Mcamara\LaravelLocalization\Traits\LoadsTranslatedCachedRoutes;

class RouteServiceProvider extends ServiceProvider
{
use \Mcamara\LaravelLocalization\Traits\LoadsTranslatedCachedRoutes;
use LoadsTranslatedCachedRoutes;
}
```

**after laravel 11**

In your App's `AppServiceProvider`, use the `CachedTranslatedRouteLoader` class in register method:
**After Laravel 11**
For Laravel 11 and newer, add the `LoadsTranslatedCachedRoutes` trait to your `AppServiceProvider`, and register the cached routes within the boot method:

```php
<?php

use Illuminate\Foundation\Support\Providers\RouteServiceProvider;
use Illuminate\Support\ServiceProvider;
use Mcamara\LaravelLocalization\Traits\LoadsTranslatedCachedRoutes;

class AppServiceProvider extends ServiceProvider
{
use \Mcamara\LaravelLocalization\Traits\LoadsTranslatedCachedRoutes;
/**
* Bootstrap any application services.
*/
{
use LoadsTranslatedCachedRoutes;

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

// ...
}
}
```

Once configured, use the following command to cache your localized routes instead of `php artisan route:cache`:
```bash
php artisan route:trans:cache
```

To clear the localized route cache, use:
```bash
php artisan route:trans:clear
```

For more details see [here](CACHING.md).
To get a list of routes for a given locale, use:
```bash
php artisan route:trans:list {locale}

# Example:
php artisan route:trans:list en
```

## Common Issues

Expand Down
Loading