Reputation: 657
I am trying to implement multi-language URLs. Thus I want to have URLs like:
/de/ueber-uns/kontakt
and /en/about-us/contact
So far so good, I use App::before()
in filters.php to check the locale given. I think I then need a route in routes.php for every controller action in every language.
So I thought of dynamically creating the file routes.php. All I would need for it is to know how I can access all available controllers or get all registered routes in code (like artisan routes but not with CLI).
So the questions are:
Thank you in advance!
Upvotes: 4
Views: 2100
Reputation: 657
I ended up doing the following:
1) Routes in routes.php are dynamically created with a custom artisan command. It parses all Controllers and creates routes for every action in every language that is supported. The language string is handled with routes like Route::get('{lang}/customer/login', 'CustomerController@getLogin')->where('lang', '[a-z]{2}'). This way users can just change the language string and the site will load in the correct language (if supported). Routes for different languages all lead to the same controller action. For these languages except english, I need translations (routes.php in /app/lang).
2) a before filter for those controllers whose actions get translated is set in constructor. It basically checks if the language string is valid and replaces it if not. The chosen language will be set in the session.
I hope anybody can use it :)
Upvotes: 4