T
T
Tutucu2020-04-13 17:27:41
Laravel
Tutucu, 2020-04-13 17:27:41

Why doesn't localization work in Laravel routing on Ubuntu 18.04 Apache?

After transferring the site from openserver to ubuntu, the site starts to give a 404 error on pages that have localization in the URL:

Route::group(
  [
                //LocalizationService::locale() - возвращает код языка из url
    'prefix'=>LocalizationService::locale(),
    'middleware'=> ['setLocale']
  ],
  function ()
  {
    /** Главная страница*/
    Route::get('/', '[email protected]', 'index');

    /** Категории*/
    Route::get('/{aliasCategory}', '[email protected]', 'category')->name('front.category');

    /** Страница записи*/
    Route::get('/{aliasCategory}/{aliasArticle}', '[email protected]', 'article')->name('front.category.article');
  }

);
//site.ru/ru - вернёт ошибку 404

If you strictly write the prefix on ru, then everything will be fine, the page will be displayed:
Route::group(
  [
    'prefix'=>'ru',
    'middleware'=> ['setLocale']
  ],
  function ()
  {
    /** Главная страница*/
    Route::get('/', '[email protected]', 'index');

    /** Категории*/
    Route::get('/{aliasCategory}', '[email protected]', 'category')->name('front.category');

    /** Страница записи*/
    Route::get('/{aliasCategory}/{aliasArticle}', '[email protected]', 'article')->name('front.category.article');
  }

);
//site.ru/ru - отобразит страницу

Wherein :
dd(LocalizationService::locale()); //Возвращает 'ru'
//site.ru/ru - отобразит 'ru'

Pages like:
//site.ru/admin/article
//site.ru/login
work fine
What's the problem? On openserver, everything works without dancing with a tambourine, I transfer it with the help of envoy.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
Tutucu, 2020-04-13
@Tutucu

I miraculously solved the problem, it was all about caching routes : when deploying, I took commands from here:
https://laravel.com/docs/5.8/deployment#autoloader... , the trick is that the command
php artisan route:cache
caches prefixes in routers and code

Route::group(
  [
                //LocalizationService::locale() - возвращает код языка из url
    'prefix'=>LocalizationService::locale(),
    'middleware'=> ['setLocale']
  ],
  function ()
  {
    
  }

);

cached like this:
Route::group(
  [
                //LocalizationService::locale() - т.к. url нет, то вернёт и сохранит в кеш пустое значение
    'prefix'=> '',
    'middleware'=> ['setLocale']
  ],
  function ()
  {
    
  }

);

So if there are dynamic prefixes and values ​​in the routes, you can't set the cache. And to disable the enabled caching, you need to enter the command in the terminal:
php artisan route:clear

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question