Ozal  Zarbaliyev
Ozal Zarbaliyev

Reputation: 638

Laravel route segment

I wanna write a route Route::get('/{lang}/home', 'ExampleController@get_home'), so

so lang maybe exists or not?

How can I do this?

Upvotes: 0

Views: 486

Answers (2)

rkj
rkj

Reputation: 8287

Laravel doesn't allow optional parameter in middle of route. However, you can resolve it by adding 2 route like this

Route::get('/home', 'ExampleController@get_home')
Route::get('/{lang}/home', 'ExampleController@get_home')

Controller (add $lang optional param in your controller action)

class ExampleController extends Controller {

   public function get_home(Request $request, $lang = null){
     ...
   }

}

Upvotes: 2

train_fox
train_fox

Reputation: 1537

Update your route to:

Route::get('/{lang?}/home', 'ExampleController@get_home')

Upvotes: 0

Related Questions