Skip to content

How to handle conflicting path parameters like /new, /:id #721

@danielcompton

Description

@danielcompton

I'd like to create a routing system similar to Rails, e.g. /users, /users/123, /users/new. I haven't been able to figure out how to do this with Reitit. I tried this which fails

(let [rtr (r/router ["/users" 
                     ["" :user/list]
                     ["/new" :user/new]
                     ["/:id" :user/detail]])]
  [(r/match-by-path rtr "/users")
   (r/match-by-path rtr "/users/new")
   (r/match-by-path rtr "/users/123")])

Execution error (ExceptionInfo) at reitit.exception/exception (exception.cljc:19).
Router contains conflicting route paths:

-> /users/new 
-> /users/:id 

I tried setting :conflicting in various combinations on these routes but I couldn't find any combination that worked correctly.

(let [rtr (r/router ["/users"
                     ["" :user/list]
                     ["/new" :user/new {:conflicting true}]
                     ["/:id" :user/detail]])]
  [(r/match-by-path rtr "/users")
   (r/match-by-path rtr "/users/new")
   (r/match-by-path rtr "/users/123")])

Is this kind of routing supported? I'd like to be able to specify that :user/detail has a numeric :id so it could be disambiguated from /new. Inside :user/detail I could check for a path param of new and dispatch back out to :user/new, but that doesn't feel very clean.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions