Passing parameters to the router line

Hello, sorry to bother you, but could you tell me if it is possible to build a URL address in which the :city parameter will specify different cities, for example: /fly-to-amsterdam or /fly-to-paris

I tried to make a router like /fly-to-:city, but it works as a static address.

I tried to use wildcard params and then define the parameter using the regular expression router.get('*', (ctx) => {...}). This works but breaks the static.

The /fly-to/:city format works, but does not suit the customer.

Join The Discussion! (3 Replies)

Please sign in or sign up for free to join in on the dicussion.

  1. Commented 26 days ago

    Hi Oliver!

    Partial route parameters, like /fly-to-:city aren't supported by the AdonisJS Router at present. However, you can use a vague route parameter in conjunction with a route param validator to make this work.

    router.get('/:flyToCity', [Controller, 'handle']).where('flyToCity', /^fly-to-/)
    Copied!

    The where here is applying a validator to the route parameter, stating that the parameter should start with "fly-to-" in order for the route to match the definition.

    We have a lesson where we cover this using user profiles in our Let's Learn AdonisJS 6 series if you'd like to learn more!

    1

    Please sign in or sign up for free to reply

    1. Commented 25 days ago

      Thank you very much! Your answer solved my problem.

      1

      Please sign in or sign up for free to reply

      1. Commented 25 days ago

        Anytime! Awesome, glad to hear you’re all set Oliver!

        1

        Please sign in or sign up for free to reply