Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

rutcreate's avatar

Multiple language route name

In web.php, I define route like this.

Route::name('contact.index')->get('contact', 'ContactController@index');
Route::name('contact.send')->post('contact', 'ContactController@send');

In ContactController.php

public function send()
{
    // Do send mail.
    return redirect()->route('contact.index');
}

Code above works perfectly fine.

My question is if I need contact page to display in another language, What name I should use for all language because in send() method I hard coded to contact.index.

0 likes
5 replies
ModestasV's avatar

Route names are for internal usage and naming not for translations. Routes must not have names in every language rather they must each have a unique name attached to a specific route. Your users won't ever see a string of contact.index as long as you pass it in route('contact.index') or any other helper which then makes it into a HTML link containing the URL not the route name.

tl;dr;: Routes should have a fixed name and never be translated into each language your system supports as they are not translations or attached to app locale.

1 like
stevenbauman's avatar

In addition to @ModestatsV's answer (which is 100% correct), you can translate the actual path's of your routes using the __() helper:

Route::name('contact.index')->get(__('contact'), 'ContactController@index');
Route::name('contact.send')->post(__('contact'), 'ContactController@send');

https://laravel.com/docs/5.4/localization#retrieving-translation-strings

Best part is, you can perform the translation at any time, and 'contact' will be returned if no translation is found.

rutcreate's avatar

@ModestasV Yes! you are right. But what if I wanna support multiple language for contact (/es/contact) and I want to refer to their names. It wises to do as below?

routes/web.php

Route::name('contact.index')->get('contact', 'ContactController@index');
Route::name('contact.send')->post('contact', 'ContactController@send');
Route::name('es.contact.index')->get('contact', 'ContactController@index');
Route::name('es.contact.send')->post('contact', 'ContactController@send');

app/Http/Controllers/ContactController.php

public function contact(Request $request)
{
    $this->sendmail($request);

    
    $routeName = get_route_name('contact.index');
    return redirect()->route($routeName);
}

helpers.php

public function get_route_name($name)
{
    $locale = App::getLocale();
    if ($locale != 'en') {
        $name = $locale .'.'. $name;
    }
    return $name;
}

All I want is redirect to the specific route (page) which the current language.

rutcreate's avatar

@stevenbauman So I need to add translation files.

routes/web.php

Route::name('contact.index')->get(__('routes.contact'), 'ContactController@index');
Route::name('contact.send')->post(__('routes.contact'), 'ContactController@send');

resources/lang/en/routes.php

return [
    'contact' => 'contact',
];

resources/lang/es/routes.php

return [
    'contact' => 'es/contact',
];

Am I correct?

1 like

Please or to participate in this conversation.