Search code examples
laravellaravel-8laravel-routing

laravel named resources not working properly and removes the dots from the beginning


problem:

I'm trying to add name prefixes to my route groups:

Route::middleware(['auth:sanctum', 'verified'])->prefix('dashboard')->name('dashboard')->group(function () {
    
    Route::get('', [DashboardController::class, 'dashboard']);

    // other routes
    Route::resource('estates', EstateController::class, ['names' => '.estates' ])->except('show');
    // other routes
});

it removes the .. (it should be dashboard.estates.index , etc ...)

| POST      | dashboard/estates                       | dashboardestates.store          |
| GET|HEAD  | dashboard/estates                       | dashboardestates.index          |
| GET|HEAD  | dashboard/estates/create                | dashboardestates.create         |
| DELETE    | dashboard/estates/{estate}              | dashboardestates.destroy        |
| PUT|PATCH | dashboard/estates/{estate}              | dashboardestates.update         |
| GET|HEAD  | dashboard/estates/{estate}/edit         | dashboardestates.edit           |

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

test 1:

how ever if I pass an array option to names it works fine with dots ..

Route::resource('estates', EstateController::class, ['names' => ['index' => '.estates.index'] ])->except('show');
| POST      | dashboard/estates                       | dashboardestates.store          |
| GET|HEAD  | dashboard/estates                       | dashboard.estates.index         |

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

test 2:

// web.php
Route::resource('estates', EstateController::class, ['names' => '.estates.' ])->except('show');

routes list:

| POST      | dashboard/estates                       | dashboardestates..store         |
| GET|HEAD  | dashboard/estates                       | dashboardestates..index         |
| GET|HEAD  | dashboard/estates/create                | dashboardestates..create        |
| DELETE    | dashboard/estates/{estate}              | dashboardestates..destroy       |
| PUT|PATCH | dashboard/estates/{estate}              | dashboardestates..update        |
| GET|HEAD  | dashboard/estates/{estate}/edit         | dashboardestates..edit          |

expected: dashboard.estates..index, etc

got: dashboardestates.index, etc


Solution

  • You can simplify it all to as follows:

    Route::group(['middleware' => ['auth:sanctum', 'verified']], function () {
        
        Route::get('/dashboard', [DashboardController::class, 'dashboard'])
            ->name('dashboard');
        
        Route::group(['prefix' => '/dashboard', 'as' => 'dashboard.'], function () {
            Route::resource('estates', EstateController::class)->except('show');
        });
    
    });
    

    The output of the above for named routes will be dashboard.estates.{method}.

    This could be simplified (i.e. not repeating or nesting certain things) the named route for the dashboard were to be dashboard.index rather than dashboard.