chinleung / laravel-multilingual-routes

A package to handle multilingual routes in your Laravel application.
https://github.com/chinleung/laravel-multilingual-routes-demo
MIT License
394 stars 26 forks source link

Add a slash at the end of multilingual Home routes #57

Closed monsieurbab closed 3 years ago

monsieurbab commented 3 years ago

Hello, After configuring and installing this package, I realized that Home multilingual routes don't have a trailing slash.

Example : -> Desired route: https://www.example.com/fr/ -> Route generated: https://www.example.com/fr

routes/web.php Route::multilingual('/', [ShowHomepage::class])->name('homepage');

php artisan route:list GET|HEAD | /fr | fr.homepage | Closure | web | ...

Is there a way to tell the package to leave the slash at the end only for the Home route?

Thank you for this package which is perfect for multilingual websites!

chinleung commented 3 years ago

Hey @MonsieurBab,

Is there any reason why you would like a trailing slash only for the home routes?

monsieurbab commented 3 years ago

Thank you for the quick reply!

There is no particular technical reason. This is a "URL style" that we use on our current website and we would like to keep it. None of our URLs do not contain a slash at the end except for Home which acts as the starting point of a "Language Folder" if you imagine it like that.

Do you think it's possible that you add the option to the package or do you think it would be better to manage this in our .htaccess file?

chinleung commented 3 years ago

@MonsieurBab What about the other routes though? Do they have a trailing slash?

If I'm not mistaken, Laravel supports the trailing slash by default.

So if your registered route is /fr, the /fr/ would work as well.

I could add an option to add a trailing slash but it would be for all routes managed by the package, not only the home url.

monsieurbab commented 3 years ago

No, I only need it for the Home route of each language.

By default Laravel has this rule in the .htaccess:

# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]

If you can't add the option of trailing slash for some routes only, then I think I'll put a rule in the .htaccess for our Home routes. That will be simpler ;)

Thanks for your help!

chinleung commented 3 years ago

No, I only need it for the Home route of each language.

By default Laravel has this rule in the .htaccess:

# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]

If you can't add the option of trailing slash for some routes only, then I think I'll put a rule in the .htaccess for our Home routes. That will be simpler ;)

Thanks for your help!

Oh I see. I use nginx which doesn't use the .htaccess, that's why it's working for me! Wouldn't that rule remove the trailing slash even if you register the route with a trailing slash though? 🤔


I found a workaround to register the home routes with a trailing slash:

Route::multilingual('home', '...');

And then in all your translation files resources/lang/{locale}/routes.php, you can add the following:

return [
    'home' => '/',
];

This will give you the following routes:

+------------------------+--------------+
| URI                    | Name         |
+------------------------+--------------+
|                        | en.home      |
| fr/                    | fr.home      |
+------------------------+--------------+

However, when you use the route helper, it will remove the trailing slash automatically:

localized_route('home', [], 'fr'); // https://demo.test/fr
route('fr.home'); // https://demo.test/fr

I think it'd be best if you alter the rules in your .htaccess to add the trailing slashes for your home route to achieve what you'd like!

Have a nice day. 😄