Routes are not working as expected


#1

Consider the following two routes:

Route
    .get('/permission-names/:id', 'Admin/PermissionNameController.show')
    .as('admin.permission-names.show')
    .middleware(['isRoutePermitted:BE_PERMISSION_NAMES_VIEW'])
Route
    .get('/permission-names/create', 'Admin/PermissionNameController.create')
    .as('admin.permission-names.create')
    .middleware(['isRoutePermitted:BE_PERMISSION_NAMES_CREATE'])

If I click on the link /permission-names/create then the Admin/PermissionNameController.show handler is called instead of create which is incorrect.

Do we need to define routes in a sequential way?


#2

Hey @mjangir! :wave:

This is the expected behaviour.
When you define your routes, you need to be careful on the order.

Your route file is read from the top to bottom and Adonis stops at the first route that matches all criteria.

In your example, created could be the content of the :id variable, so it stops there and calls the handler Admin/PermissionNameController.showwith created as a parameter.

You can avoid that by:

  1. Define your route in a different order (/created comes before /:id);
  2. Use a regex to force :id to be an integer.

#3

Ok I got it. Do we have route validators like putting regex for params?


#4

The current version of the Router uses path-to-regexp to generate the route.

Check their documentation to know how to add regexp to your route.