Named middlewares are called for all matching routes


#1

I have the following routes:

Route
    .get('/permission-names', 'Admin/PermissionNameController.index')
    .as('admin.permission-names.all')
    .middleware(['isRoutePermitted:BE_PERMISSION_NAMES_LIST'])
Route
    .get('/permission-names/create', 'Admin/PermissionNameController.create')
    .as('admin.permission-names.create')
    .middleware(['isRoutePermitted:BE_PERMISSION_NAMES_CREATE'])

When I call the route/permission-names/create, the middleware isRoutePermitted is called twice which means all the matching routes /permission-names and /permission-names/create.

How to exact match the route like if I call the create route, then the middleware should be called only once. Actually I want to call this middleware right before it hits the handler.


#2

Hey @mjangir! :wave:

How do you know that the middleware has been called twice?

Please, ensure that you are not also defining this middleware in a Route Group or as a global middleware.


#3

I mean to say, all the matching routes handlers are called which is the problem. For example, If I’ve two following routes:

Route.get('/permission-names', 'Admin/PermissionNameController.index')
Route.get('/permission-names/12', 'Admin/PermissionNameController.show')

With the above case, if I hit /permission-names/12 then the first route handler shouldn’t call but its happening right now.


#4

I cannot reproduce your issue and your code seems fine.

Please, create a repository with the minimum amount of code to reproduce the issue so we can try to help you.


#5

Well, it happens in case of ajax request. If I call the second URL with ajax, then the first handler also called.


#6

@romain.lanz Hey I was completely wrong in the last comment. Routes are working fine.