Download the PHP package skyraptor/laravel-localized-routes without Composer

On this page you can find all versions of the php package skyraptor/laravel-localized-routes. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package laravel-localized-routes

Laravel Localized Routes

IMPORTANT: March 2022

Support Ukraine

It's horrible to see what is happening now in Ukraine, as Russian army is bombarding houses, hospitals and kindergartens.

Please check out supportukrainenow.org for the ways how you can help people there. Spread the word.

And if you are from Russia and you are against this war, please express your protest in some way. I know you can get punished for this, but you are one of the hopes of those innocent people.


GitHub release Laravel Build Status Code Coverage Code Quality Total Downloads

ko-fi

A convenient way to set up and use localized routes in a Laravel app.

🧩 Features

✅ Requirements

📦 Install

Laravel will automatically register the ServiceProvider.

⚙️ Configure

☑️ Publish Configuration File

You will now find a localized-routes.php file in the config folder.

☑️ Supported Locales

Using Slugs

Add any locales you wish to support to your published config/localized-routes.php file:

This will automatically prepend a slug to your localized routes. More on this below.

Using Domains

Alternatively, you can use a different domain or subdomain for each locale by configuring the supported-locales like this:

☑️ Omit Slug for Main Locale

Specify your main locale if you want to omit its slug from the URL:

Setting this option to 'en' will result, for example, in URL's like this:

This option has no effect if you use domains instead of slugs.

☑️ Use Middleware to Update App Locale

By default, the app locale will always be what you configured in config/app.php. To automatically update the app locale when a localized route is accessed, you need to use a middleware.

⚠️ Important note for Laravel 6+

To make route model binding work in Laravel 6+ you always also need to add the middleware to the $middlewarePriority array in app/Http/Kernel.php so it runs before SubstituteBindings:

You can then enable the middleware in a few ways:

For every localized route, via our config file

Simply set the option to true to add the middleware to every localized route:

This will not add the middleware to non-localized routes!

OR, for every route using the web middleware group

You can manually add the middleware to the $middlewareGroups array in app/Http/Kernel.php:

OR, for specific routes

Alternatively, you can add the middleware to a specific route or route group:

☑️ Use Localizer to Detect and Set the Locale

This package can use codezero/laravel-localizer to automatically detect and set the locale.

With this option disabled, the app locale will only be updated when accessing localized routes.

With this option enabled, the app locale can also be updated when accessing non-localized routes. For non-localized routes it will look for a preferred locale in the session, in a cookie or in the browser. Additionally, it will also store the app locale in the session and in a cookie.

Enabling this option can be handy if you have, for example, a generic homepage and you want to know the preferred locale.

To enable this option, set it to true in the published config file.

This option only has effect on routes that use our SetLocale middleware.

You can review codezero/laravel-localizer, publish its config file and tweak it as needed. The only option we will override is supported-locales, to match the option in our own config file.

☑️ Set Options for the Current Localized Route Group

To set an option for one localized route group only, you can specify it as the second parameter of the localized route macro. This will override the config file settings.

🚗 Register Routes

Example:

In the above example there are 5 routes being registered. The routes defined in the Route::localized closure are automatically registered for each configured locale. This will prepend the locale to the route's URI and name. If you configured custom domains, it will use those instead of the slugs.

URI Name
/home home
/en/about en.about
/nl/about nl.about
/en/admin/reports en.admin.reports.index
/nl/admin/reports nl.admin.reports.index

If you set omit_url_prefix_for_locale to 'en' in the configuration file, the resulting routes look like this:

URI Name
/home home
/about en.about
/nl/about nl.about
/admin/reports en.admin.reports.index
/nl/admin/reports nl.admin.reports.index

⚠️ Beware that you don't register the same URL twice when omitting the locale. You can't have a localized /about route and also register a non-localized /about route in this case. The same idea applies to the / (root) route! Also note that the route names still have the locale prefix.

🔦 Localized 404 Pages and Redirecting to Localized URL's

To use these 2 features, you need to register the fallback route at the end of your routes/web.php file:

404 - Not Found

After registering the fallback route the provided controller will look for a 404 error view at resources/views/errors/404.blade.php. If this view does not exist, a normal NotFoundHttpException will be thrown. You can configure which view to use by changing the 404_view entry in the config file.

**Some background info...** By default, Laravel's `404` pages don't go trough the middleware and have no `Route::current()` associated with it. Not even when you create your custom `errors.404` view. Therefor, the locale can't be set to match the requested URL automatically via middleware. To enable localized `404` pages, you need to register a `fallback` route and make sure it has the `SetLocale` middleware. This is basically a catch all route that will trigger for all non existing URL's. Another thing to keep in mind is that a `fallback` route returns a `200` status by default. So to make it a real `404` you need to return a `404` response yourself. Fallback routes will not be triggered when: - your existing routes throw a `404` error (as in `abort(404)`) - your existing routes throw a `ModelNotFoundException` (like with route model binding) - your existing routes throw any other exception Because those routes are in fact registered, the `404` page will have the correct `App::getLocale()` set. [Here is a good read about fallback routes](https://themsaid.com/laravel-55-better-404-response-20170921).
Redirecting to Localized URL's

After registering the fallback route, you can update the config option redirect_to_localized_urls to true. The provided FallbackController will then try to redirect routes that have not been registered, to their localized version. If it does not have a localized version, a 404 will be thrown.

So the home page / would be redirected to /en if the active locale is en and the /en route exists. And /about would redirect to /en/about.

If you configured the app to omit the main locale slug, then the former redirection will obviously not happen, because the unprefixed routes exist. Instead, accessing a prefixed route with the main locale will redirect to an unprefixed URL.

So if the main locale is en, visiting /en would redirect to the home page / (unless / is a registered route). Also /en/about would redirect to /about.

🚕 Generate Route URL's

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

👎 The ugly way...

Normally you would have to include the locale whenever you want to generate a URL:

👍 A much nicer way...

Because the former is rather ugly, this package overwrites the route() function and the underlying UrlGenerator class with an additional, optional $locale argument and takes care of the locale prefix for you. If you don't specify a locale, either a normal, non-localized route or a route in the current locale is returned.

This is the new route helper signature:

A few examples (given the example routes we registered above):

Note: in a most practical scenario you would register a route either localized or non-localized, but not both. If you do, you will always need to specify a locale to get the URL, because non-localized routes always have priority when using the route() function.

🚌 Redirect to Routes

Laravel's Redirector uses the same UrlGenerator as the route() function behind the scenes. Because we are overriding this class, you can easily redirect to your routes.

You can't redirect to URL's in a specific locale this way, but if you need to, you can of course just use the route() function.

⚓️ Generate Localized Versions of the Current URL

To generate a URL for the current route in any locale, you can use this Route macro:

With Route Model Binding

If your route uses a localized key (like a slug) and you are using route model binding, then the key will automatically be localized.

If you have a route with multiple keys, like /en/posts/{id}/{slug}, then you can pass the parameters yourself (like in the example without route model binding below) or you can implement this interface in your model:

Now, as long as you use route model binding, you can still just do:

Without Route Model Binding

If you don't use route model binding and you need a localized slug in the URL, then you will have to pass it manually.

For example:

The getSlug() method is just for illustration, so you will need to implement that yourself of course.

✍🏻 Generate Signed Route URL's

Generating a signed route URL is just as easy.

Pass it the route name, the necessary parameters and you will get the URL for the current locale.

You can also generate a signed URL for a specific locale:

Check out the Laravel docs for more info on signed routes.

🌎 Translate Routes

If you want to translate the segments of your URI's, create a routes.php language file for each locale you configured:

In these files, add a translation for each segment, or for the full URI.

If an exact match is found, that will be returned. Otherwise, each segment will be translated separately. If a translation is not found, the original segment is used.

Now you can use our Lang::uri() macro during route registration:

The above will generate:

If you need to get a translation from a package, you can pass an optional translation namespace as a third parameter to Lang::uri():

Note that in order to find a translated version of a route, you will need to give your routes a name. If you don't name your routes, only the parameters (model route keys) will be translated, not the "hard-coded" slugs.

🚏 Route Parameters

Parameter placeholders are not translated via language files. These are values you would provide via the route() function. The Lang::uri() macro will skip any parameter placeholder segment.

If you have a model that uses a route key that is translated in the current locale, then you can still simply pass the model to the route() function to get translated URL's.

An example...

Given we have a model like this:

TIP: checkout spatie/laravel-translatable for translatable models.

If we have a localized route like this:

We can now get the URL with the appropriate slug:

🚴‍ Route Model Binding

If you enable the middleware included in this package, you can use Laravel's route model binding to automatically inject models with localized route keys in your controllers.

All you need to do is add a resolveRouteBinding() method to your model. Check Laravel's documentation for alternative ways to enable route model binding.

Laravel 7 and newer has an optional $field parameter that allows you to override the route key on specific routes:

The new method would then look like this:

TIP: checkout spatie/laravel-translatable for translatable models.

🗃 Cache Routes

In production you can safely cache your routes per usual.

🚧 Testing

☕️ Credits

🔓 Security

If you discover any security related issues, please e-mail me instead of using the issue tracker.

📑 Changelog

A complete list of all notable changes to this package can be found on the releases page.

📜 License

The MIT License (MIT). Please see License File for more information.


All versions of laravel-localized-routes with dependencies

PHP Build Version
Package Version
Requires php Version ^7.1|^8.0
0.0.0/composer-include-files Version ^1.5
codezero/laravel-localizer Version ^1.1
illuminate/support Version ^5.6|^6.0|^7.0|^8.0|^9.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package skyraptor/laravel-localized-routes contains the following files

Loading the files please wait ....