Skip to content

Commit 572b0f3

Browse files
committed
Add localized 404 instructions to README
1 parent 7122e2f commit 572b0f3

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,36 @@ If you set `omit_url_prefix_for_locale` to `'en'` in the configuration file, the
259259
You can't have a localized `/about` route and also register a non-localized `/about` route in this case.
260260
The same idea applies to the `/` (root) route! Also note that the route names still have the locale prefix.
261261

262+
### 🔦 Localized `404` Pages
263+
264+
By default, Laravel's `404` pages don't go trough the middleware and have no `Route::current()` associated with it.
265+
Not even when you create your custom `errors.404` view.
266+
Therefor, the locale can't be set to match the requested URL automatically via middleware.
267+
268+
To enable localized `404` pages, you need to register a `fallback` route
269+
and make sure it has the `SetLocale` middleware and a name of `404`.
270+
This is basically a catch all route that will trigger for all non existing URL's.
271+
272+
```php
273+
Route::fallback(function () {
274+
return response()->view('errors.404', [], 404);
275+
})->name('404')->middleware(\CodeZero\LocalizedRoutes\Middleware\SetLocale::class);
276+
```
277+
278+
> Because you might use a `fallback` route for another purpose,
279+
this package will only consider it to be a `404` if you name the route `404`.
280+
281+
Another thing to keep in mind is that a `fallback` route returns a `200` status by default.
282+
So to make it a real `404` you need to return a `404` response yourself.
283+
284+
Fallback routes will not be triggered when:
285+
286+
- your existing routes throw a `404` error (as in `abort(404)`)
287+
- your existing routes throw a `ModelNotFoundException` (like with route model binding)
288+
- your existing routes throw any other exception
289+
290+
Because those routes are in fact registered, the `404` page will have the correct `App::getLocale()` set.
291+
262292
### 🚕 Generate Route URL's
263293

264294
You can get the URL of your named routes as usual, using the `route()` helper.

0 commit comments

Comments
 (0)