skip to Main Content

I was using following Route declaration to pass language action to the routes.

Route::get('/de', ['uses' => 'PageController@showHomepage', 'language' => 'de']);

And in controller, I was able to get this value like this:

$action = $request->route()->getAction();
// $action['language'] gives the given value here..

But with callable syntax, I couldn’t find how can I do the same thing. When using the following type of route definition:

Route::get('/', [PageController::class, 'showHomepage']);

How can I pass the language to new declaration? Is that a way to pass arguments to callable in Route::get() ?

Something like this

Route::get('/', ([PageController::class, 'showHomepage'],'languagecode_parameter_here'));

Thank you very much.

PS: If you are asking why do I need such custom parameter, here is more details about the need: https://github.com/laravel/framework/discussions/44839

2

Answers


  1. You can’t.

    And this is not appropirate way to localize your routes in laravel.

    If you want to set localization for your laravarl application there is a package fir that and the name of pakcage in github is mcamara/laravel-localization it is one of the best localization packages.

    Package github link

    Login or Signup to reply.
  2. Reading your last comment, I would suggest you to create a middleware to set the language. You can call your route with the middleware that is actually "right" for it (or group the routes with that middleware). This way, your project is more future-orientated and organized.

    See the docs https://laravel.com/docs/master/middleware

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search