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

lwvvliet's avatar

Routing url to controller/action

Hi,

I'm new to Laravel and I want to setup a routing that takes a controller and action from the url and automatically routes it to the corresponding controller and action

So instead of this:

Route::get('/', 'IndexController@index');

I would like to do something like:

Route::get('{controller}/{action}', '{controller}Controller@{action}');

So if the url would be /user/posts that it would automatically go to the posts() action in the UserController without specifying routes each controller and action separately. Is that possible? Is it also possible for multi-language websites? For example that /user/posts (english) and /utilisateur/poste (french) would both be automatically routed to the same posts() action in the UserController.

Thanks.

0 likes
12 replies
rawilk's avatar

IMO this is just a bad idea. As your project scales, it's going to get more confusing. How will you even know what are all the routes that you have if you do this?

1 like
lwvvliet's avatar

Hi @wilk_randall , thanks for your response.

I'm asking this because I'm used to this kind of behavior from Zend Framework. There urls were automatically parsed into module/controller/action/params bits and then requests were routed to the corresponding controller and action. This also worked for translated urls.

Do you propose to declare a separate route for each page I want to create on my website? Do you know how this would work if urls can be in different languages?

vladshoob's avatar

I made something similar recently. I needed very nice product urls (site.com/product_alias).

In the very end of your routes you may do something like this.

Route::get({controller}{action}, 'RoutesController@redirect');

In your RoutesController you will need something like this:

public function redirect($controller, $action) {
\App\Http\Controllers\{$main}Controller::{action};

// I don't really know how to make this line work, but I think it is possible
}

As for me, I may turn it around if multilanguage is required. In routes file

Route::get('/{language}/contact', 'SiteController@contact');
Route::resource('/{language}/products', 'ProductsController');

And in your controller store language in user session and present information in required languages.

lwvvliet's avatar

@vladshoob Thanks for your response. I will try your approach with the redirect in the RoutesController.

Actually I don't want a language slug in the url. In my case the multi language site means that the site runs on different domains that each have their own language and locale.

For example a page that contains explanation will have these urls:

www.example.co.uk/explanation
www.example.de/erklarung
www.example.fr/explication

Even in case of a language slug in the url I would still like the whole url to be in the same language. So for French I don't want the url to be /fr/product but instead /fr/produit

rawilk's avatar

@lwvvliet - I usually create the routes my self for each page, unless they are dynamically created pages such as product pages. For the language, it is not necessary to put the language in the URL, just store it in the session or in the database if you want.

1 like
vladshoob's avatar

You may set locale on .env file for each website. Make custom config('app.locale') for this. I assume each domain will require separate codebase.

Snapey's avatar

The redirect is a bad idea. Fine for last resort matching of incorrect urls, but if you took this approach, every page would be redirected which means two requests for the client and not one.

It also breaks anything other than get requests.

Just list the routes and stop looking for shortcuts where none is required

2 likes
lwvvliet's avatar

@snapey Ok thanks for your response. I will use 1 codebase for the different domains. Does that mean I have to specify routes to the same page in all different languages? Like this I mean:

Route::get('/explanation', 'ExplanationController@index');
Route::get('/erklarung', 'ExplanationController@index');
Route::get('/explication', 'ExplanationController@index');
Snapey's avatar

yes if the names of the routes are different in each language.

They should probably also go through to different controller methods if the response needs to be different.

vladshoob's avatar

And then maybe use defined names for routes.

Route::get('/explanation', 'ExplanationController@index')->name('en.explanation');
Route::get('/erklarung', 'ExplanationController@index')->name('fr.explanation');
Route::get('/explication', 'ExplanationController@index')->name('sp.explanation');

So later it will be possible to create urls in blade.

{{ route(App::getLocale() . '.explanation') }}

And my redirect function actually does not suppose to redirect. It was just a bad naming. It supposes to use another function in some controller. And that function will deal with rendering.

lwvvliet's avatar

Ok thanks I will try it. @vladshoob Do you know if your named routes approach is the same as this?

$url = action('ExplanationController@index');

Will this $url contain a url in the right language?

And how can I redirect to a translated url from inside a controller?

martinbean's avatar

@lwvvliet Laravel strongly advocates you explicitly define your routes, instead of “auto-routing” like Zend or CodeIgniter.

Please or to participate in this conversation.