Laravel route segment


Laravel route segment



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


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



so lang maybe exists or not?



How can I do this?





Use a regex: laravel.com/docs/5.6/…
– DigitalDrifter
Jun 29 at 9:09




2 Answers
2



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){
...
}



}



Update your route to:


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





not works, if lang exists in url works else gives page not found
– Ozal Zarbaliyev
Jun 29 at 9:03





I think you need to change {lang?} and home places.
– train_fox
2 days ago






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

Opening a url is failing in Swift

Export result set on Dbeaver to CSV