Core: Route.resource in Route.group problem

Created on 28 Nov 2016  路  5Comments  路  Source: adonisjs/core

Route.group('public', () => {
  Route
  .resource('objects', 'api/public/ObjectsController')
  .only('store')
  .addMember('metrics', ['POST'])
  .addMember('actions', ['POST'])
  .as('public.objects')
})
.prefix('/api/v1/public')

Route.group('api', () => {
  Route
  .resource('objects', 'api/ObjectsController')
  .except('edit', 'create')
  .addMember('metrics')
  .addMember('users')
  .middleware('auth')
})
.prefix('/api/v1')

it generated two identical name

| Method | URI | Action | Middlewares | Name
|------------|------------------------------|----------------------------------------------|---------------------|------------------
| POST | /api/v1/public/objects | api/public/ObjectsController.store | | objects.store
| POST | /api/v1/objects | api/ObjectsController.store | auth | objects.store

Most helpful comment

@belozer Prefixing the group name will be a breaking change for others since their route names will change.

I will queue this up for the major release. For now I can suggest to make use of as method of resource as follows.

Route
.resource('objects', 'api/public/ObjectsController')
.only('store')
.addMember('metrics', ['POST'])
.addMember('actions', ['POST'])
.as({
  store: 'public.objects.store',
  metrics: 'public.objects.metrics',
  actions: 'public.objects.actions'
})

All 5 comments

Yup, it is a known issue. Will fix it by prefix the group name since that is unique.

@belozer Prefixing the group name will be a breaking change for others since their route names will change.

I will queue this up for the major release. For now I can suggest to make use of as method of resource as follows.

Route
.resource('objects', 'api/public/ObjectsController')
.only('store')
.addMember('metrics', ['POST'])
.addMember('actions', ['POST'])
.as({
  store: 'public.objects.store',
  metrics: 'public.objects.metrics',
  actions: 'public.objects.actions'
})

Route.group could become an hidden extension to allow an object instead a string like Route.group of Laravel

        Route::group([
            'middleware' => ['web', 'admin'],
            'namespace' => 'App\Http\Controllers\Admin',
            'prefix' => 'admin',
            'as' => 'admin.',
        ], function ($router) {
            require base_path('routes/admin.php');
        });

the option property "as" set the prefix for names inside the group

Removed addMember and addCollection, they are super confusing and doesn't add much value either

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

PC-HUB picture PC-HUB  路  4Comments

krunaldodiya picture krunaldodiya  路  3Comments

milosdakic picture milosdakic  路  3Comments

devcaststudio picture devcaststudio  路  3Comments

themodernpk picture themodernpk  路  3Comments