search

Home  >  Q&A  >  body text

Using "resource"; method in Route::controller(<class>)->group(...)

I'm editing my application routing files to make them as neat as possible. So I've done this in my "web.php":

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

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粉475315142P粉475315142406 days ago763

reply all(1)I'll reply

  • P粉022501495

    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

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    $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']);

    });

    reply
    0
  • Cancelreply