Mikhail IVANOV
Mikhail IVANOV

Reputation: 21

Laravel route group prefix - variable not working

in web.php :

Route::group(['middleware'=>['checklang','checkmoney']],function(){
    Route::get('/', function () {
    return redirect('/'.session()->get('lang'));
    });
    
    Route::group([
    'prefix' => '{locale}',
    'where'=>['locale'=>'[a-zA-Z]{2}']],
 function() {

    Route::get('/tour/{id}','HomeController@getTours');
});
});

in HomeContoller :

   public function getTours($id){
 
    dd($id);
}

when trying to access url : example.com/en/tour/5 getting result en , but should be 5

Where is a problem and how to solve it?

Upvotes: 1

Views: 4190

Answers (1)

Tim Lewis
Tim Lewis

Reputation: 29258

Your route has 2 variables, {locale} and {id}, but your Controller method is only referencing one of them. You need to use both:

web.php:

Route::group(['prefix' => '{locale}'], function () {
  ...
  Route::get('/tour/{id}', 'HomeController@getTours');
});

HomeController.php

public function getTours($locale, $id) {
   dd($locale, $id); // 'en', 5
}

Note: The order of definition matters; {locale} (en) comes before {id} 5, so make sure you define them in the correct order.

Upvotes: 4

Related Questions