Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for Multiple Route Parameters in a Single Segment (e.g., /set-alarm-for-:hour-:minute-:amOrPm) #94

Open
mdthansil opened this issue Nov 24, 2024 · 2 comments
Labels
Good First Issue Want to contribute? Just filter by this label Type: Enhancement Improving an existing feature

Comments

@mdthansil
Copy link

Package version

6.14

Describe the bug

AdonisJS currently lacks the ability to define multiple parameters within a single segment of the URL (e.g., :param1-:param2-:param3). This feature would be highly beneficial for creating more descriptive and user-friendly URLs.

Example in Laravel:

In Laravel, it is possible to define a route like this:

Route::get('/set-alarm-for-{hour}-{minute}-{amOrPm}', [AlarmController::class, 'setAlarm']);

This allows URLs such as:

/set-alarm-for-10-30-am

In the above example:

  • hour is 10
  • minute is 30
  • amOrPm is am

Attempt in AdonisJS:

Currently, AdonisJS doesn't support this syntax. For instance, the following AdonisJS route definition:

router.get('/set-alarm-for-:hour-:minute-:amOrPm', (ctx) => {})

Results in a routing error or unexpected behavior.

Feature Request:

Support for this feature would align AdonisJS with the capabilities of other frameworks like Laravel and enhance its ability to handle clean, descriptive URLs. This functionality could be implemented by allowing multiple dynamic parameters within a single segment, separated by delimiters like -.


Benefits:

  1. Improved Readability: More user-friendly and descriptive URLs.
  2. Consistency with Other Frameworks: Developers transitioning from Laravel or similar frameworks would find this familiar.
  3. Enhanced Functionality: Enables more flexible routing patterns for complex URL structures.

Suggested Syntax:

An example route definition in AdonisJS:

router.get('/set-alarm-for-:hour-:minute-:amOrPm', (ctx) => {})

Parameters Accessible in Controller:

The parameters should be accessible like this in the handler:

router.get('/set-alarm-for-:hour-:minute-:amOrPm', ({ params }) => {
  const { hour, minute, amOrPm } = params
  console.log(hour, minute, amOrPm)
})

Current Workaround:

To achieve a similar effect, developers must split the parameters into separate segments, such as:

router.get('/set-alarm/:hour/:minute/:amOrPm', (ctx) => {})

This, however, results in less descriptive URLs:

/set-alarm/10/30/am

Reproduction repo

No response

@mdthansil mdthansil changed the title Support for Multiple Route Parameters in a Single Segment (e.g., /set-alarm-for-{hour}-{minute}-{amOrPm}) Support for Multiple Route Parameters in a Single Segment (e.g., /set-alarm-for-:hour-:minute-:amOrPm) Nov 25, 2024
@RomainLanz RomainLanz added Type: Enhancement Improving an existing feature Good First Issue Want to contribute? Just filter by this label labels Nov 26, 2024
@RomainLanz RomainLanz transferred this issue from adonisjs/core Nov 26, 2024
@Barabasbalazs
Copy link

From what I gather by looking at the code it seems that the actual URL parsing is done by this package: https://github.com/poppinss/matchit

They way it seems to work is to split the URL string into separate tokens based on a couple of rules.
One of the rules is that the different segments are separated from each other by /.
Then if the parser encounters some special characters like : it knows that a dynamic value will continue.

So if we split the parameters with / the parser will recognize these tokens:
parser_separated

And if we split them based on @mdthansil recommendation the parser will only recognize 1 parameter:

parser_not_separated

It seems to me that in order to make this feature a reality the parsing package has to be modified so that it can split the url's tokens based not only with the / character.

This is just my understanding of the issue. Please correct me if I'm wrong

@thetutlage
Copy link
Member

@Barabasbalazs Yup that's how the router parser works.

I will not immediately change the parser logic, since this is one very niche use-case and can be achieved without modifying the routing layer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Good First Issue Want to contribute? Just filter by this label Type: Enhancement Improving an existing feature
Projects
None yet
Development

No branches or pull requests

4 participants