G
G
Grigory Vasilkov2019-10-14 09:33:47
Laravel
Grigory Vasilkov, 2019-10-14 09:33:47

In Laravel 6, how to add a language prefix to Route:: with default language support?

Writing:

Route::pattern('locale', '(?:/|/en|/ru)');
Route::get('{locale}', '[email protected]');
Route::get('{locale}/page', '[email protected]');

then in the terminal:
php artisan route:cache
First, I immediately get an error from another generally left module related to the fact that for some reason its ServiceProvider is restarted. This is strange because in my registration commands are executed that must be executed strictly 1 time and the module throws an exception that it cannot be called again.
Tried to move to boot() or to register() - anyway, with artisan the provider is called twice for some reason, despite the fact that the application does not restart. The problem is generally fixed by a crutch with a static bool variable "isRegistered". How to edit?
I get the following behavior:
1. /index - works
2. /ru/index - works
3. /en/index - works
4. /ru/page - works
5. /en/page - works
6. /page - doesn't work
Any hacks?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
G
Grigory Vasilkov, 2019-10-22
@gzhegow

today, by some miracle, with a tambourine and such and such a mother figured out

Route::pattern('locale', '|ru/|en/');
Route::get('{locale}route', 'action');
// /(?P<locale>(?:|en/|ru/))route

Then, to set the locale, we cut off the right oblique. So I once did it in pure php, it worked here too, otherwise it turned out either two slashes, then none
To make a redirect after comparing with a cookie or session, the slash must be returned to its place

E
Evgeny Kylin, 2019-10-14
@palkan_karabov

Route::get('{locale?}/page', '[email protected]')->where(['locale' => '^[a-z][a-z]$']);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question