How can I prevent the Route prefix from being passed to all the child routes controller functions as the first parameter? It is messing with the $id parameter of my child routes!
Route::prefix('{locale?}')->middleware(['locale.default', 'locale'])->group(function() { Route::get('/car/{id}', [ 'uses' => 'CarController@getCar', 'as' => 'car', 'middleware' => 'auth' ])->where(['id' => '[0-9]+']); }); In CarController's getCar() function, the id parameter is getting the locale (en, fr, it) instead of the id of the car being passed.
public function getCar($id) { $car = Car::where('id', $id)->firstOrFail(); } To fix this, I must do:
public function getCar($locale, $id) { $car = Car::where('id', $id)->firstOrFail(); } Is there a way to simply prevent the passing of $locale to the child route's controller functions so that I don't have to add $locale as the first parameter to every function?
If I change the getCar() parameter from $id to $request of type Request, I can do this and it works. Is this a good solution?
public function getCar(Request $request) { $car = Car::where('id', $request->id)->firstOrFail(); } The Laravel 5.6 docs seems to say that this can be done: https://laravel.com/docs/5.6/requests
Accessing The Request Via Route Closures
You may also type-hint the Illuminate\Http\Request class on a route Closure. The service container will automatically inject the incoming request into the Closure when it is executed:
use Illuminate\Http\Request;
Route::get('/', function (Request $request) { //... });
Can anyone confirm this please? Thanks!