I'm editing my application routing files to make them as neat as possible. So I've done this in my "web.php":
Route::controller(AlquilerController::class)->group(function ($hola) { //Debug lines error_log("This"); error_log(get_class($this)); //Illuminate\Routing\RouteFileRegistrar. error_log("hola"); error_log(get_class($hola)); //Illuminate\Routing\Router. Route::get('alquileres/busqueda', 'busqueda')->name('alquileres.busqueda'); Route::get('alquileres/busqueda_texto', 'busquedaTexto')->name('alquileres.busqueda_texto'); Route::middleware('auth')->get('alquileres/mis_alquileres', 'mis_alquileres')->name('alquileres.mis_alquileres'); Route::post('/alquileres/filter_texto', 'filterTexto')->name('alquileres.filter_texto'); Route::get('/alquileres/filter_texto', 'filterTexto')->name('alquileres.filter_texto_url'); Route::post('/alquileres/filter', 'filter')->name('alquileres.filter'); Route::get('/alquileres/filter', 'filter')->name('alquileres.filter_url'); Route::resource('alquileres',AlquilerController::class)->parameters(['alquileres' => 'alquiler']); });
Although this works fine, I find this line to be redundant:
Route::resource('alquileres',AlquilerController::class)->parameters(['alquileres' => 'alquiler']);
, because I repeat the text "AlquilerController::class", Both are in the parameter 'Route::controller' and then the second parameter of 'Route::resource'.
Is there any way to avoid this? I tried doing some "error_log" at the beginning of the "group" callback body just to see what I got, but I can't find a solution.
Thank you so much!
P粉0225014952024-04-05 00:55:16
How to declare a variable with the value of the controller class before the routing group and then use the variable in the routing group class to avoid duplication
$controllerClass = AlquilerController::class; Route::controller($controllerClass)->group(function () use ($controllerClass) { Route::get('alquileres/busqueda', 'busqueda')->name('alquileres.busqueda'); Route::get('alquileres/busqueda_texto', 'busquedaTexto')->name('alquileres.busqueda_texto'); Route::middleware('auth')->get('alquileres/mis_alquileres', 'mis_alquileres')->name('alquileres.mis_alquileres'); Route::post('/alquileres/filter_texto', 'filterTexto')->name('alquileres.filter_texto'); Route::get('/alquileres/filter_texto', 'filterTexto')->name('alquileres.filter_texto_url'); Route::post('/alquileres/filter', 'filter')->name('alquileres.filter'); Route::get('/alquileres/filter', 'filter')->name('alquileres.filter_url'); Route::resource('alquileres', $controllerClass)->parameters(['alquileres' => 'alquiler']); });