How to create bilingual route URLs in Laravel

Based on this thread , I have tried to implement an additional English language for the site, the default French and does not use the prefix, so something like , and switch to English would be , I would like to have the URL-address for the contact pages such as and for English and French versions respectively. www.website.comwww.website.com/en/www.website.com/en/contactwww.website.com/contact

My current .php routes

if (Request::segment(1) == 'en') {
    App::setLocale(Request::segment(1));
    Config::set('app.locale_prefix', Request::segment(1));
}
else {
    App::setLocale('fr');
    Config::set('app.locale_prefix', '');
}

Route::group(array('prefix' => Config::get('app.locale_prefix')), function()
{
    Route::get(
        '/',
        function () {
            //return "main page - ".App::getLocale();
            return view('index');
        }
    );
    Route::get(
        '/contact/',
        function () {
            return view('contact');
        });
});

My header file in which the flag icon to switch the language

    @if (Lang::locale() == 'fr')
        <a href="{{ url('/en/' . Request::segment(1)) }}"><img src="{{asset('images/GB.png')}}"></a>
    @elseif (strcasecmp(Request::segment(1), 'en') == 0 && Request::segment(2) != NULL)
        <a href="{{ url( Request::segment(2)) }}"><img src="{{asset('images/FR.png')}}"></a>
    @else
        <a href="{{ url( '/') }}"><img src="{{asset('images/FR.png')}}"></a>
    @endif

and method for generating urls

<a class="block-title" href="{{ (strcasecmp(Request::route()->getPrefix(), '/en') == 0) ? url('en/contact') : url('/contact') }}">CONTACT</a>

I would like to know a cleaner way to generate them and how can I get for the English URL of the main page www.website.com/en/insteadwww.website.com/en

Thank you so much!

+4
2

URL-:

yor:

//this route is called 'contact_route'
Route::get('/contact/', ['as' => 'contact_route', function () 
{
    return view('contact');
}]);

, , URL- , : route('contact_route')

:

<a class="block-title" href="{{ route('contact_route') }}">CONTACT</a>

docs

.

, Laravel .htaccess , URL-, :

RewriteRule ^(.*)/$ $1 [L,R=301]

(.*) ^ /$ , . , , , .htaccess

+1

Route::group(['prefix' => 'en', 'namespace' => '\English'], function () {
  Route::get('contact', [
    'as'   => 'en.contact',
    'uses' => 'ContactController@contactUs',
  ]);
});

Route::group(['prefix' => 'fr', 'namespace' => '\French'], function () {
  Route::get('contact', [
    'as'   => 'fr.contact',
    'uses' => 'ContactController@contactUs',
  ]);
});
0

Source: https://habr.com/ru/post/1649088/


All Articles