-
-
Notifications
You must be signed in to change notification settings - Fork 7.7k
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 generation of URLs for routes including URL params #1030
Comments
Could you share more details about your idea? |
When we are developing web applications it happens frequently that a view has to return a redirect to another view, or we have to include a link in templates to another view. It would be unfortunate if we had to hardcode these URLs all over the place. We can make typos and if in the future we want to change the route url we would need to propagate these changes. Moreover, this method can take params when the route takes URL params and this provides a check for incorrect number of params passed. |
I like the idea. ASP.net MVC has something called UrlHelper, perhaps you can look its documentation to realize how to implement it |
It exists in Django, Phoenix and Rails, for example. It is very useful. |
This is a PR against express’s router that aims to add a similar functionality pillarjs/router#36. I’m on holidays right now but when I’m back to work I plan to help merge this PR. |
I like the idea, Should i implement that in Nest-Router ? |
@edevil not sure if we really have to care about express in this case. Nest has its own mechanism which responsibility is to reflect all routes. I think we could reuse it effortlessly :) |
@kamilmysliwiec Ah, great. Since Nest makes use of express I thought it would be useful. So, as I understand it, Nest has its own router, Nest-Router, and it is there that this functionality should be implemented instead of express’s router? |
No no. Nest makes use of the express router, but we don't really have to affect express ecosystem at all. It should be pretty easy to build auto-generation of all registered paths using |
How would they be specified though? I'm used to something like this: @Controller('cats', 'not_cats')
class CatsController {
@Get('', 'list') // probably not '', but something else
getAll() {}
@Get(':id')
show() {}
} and then being able to do:
|
Maybe create a Example: @Controller('cats')
class CatsController {
@Get()
@Named('cats')
getAll() {}
} or you could pass in the controller and its method name instead. Example: @Controller('cats')
class CatsController {
@Get()
getAll() {}
@Get(':id')
show() {}
} then something along the lines: // For params only
// produces: <prefix?>/cats/1
urlGenerator.path(CatsController, 'show', 1, ...);
// For query and params using object
// produces: <prefix?>/cats/1?project=Nest
urlGenerator.path(CatsController, 'show', {
param: {
id: 1,
},
query: {
project: 'Nest',
}
}); or both and use the reflect value of urlGenerator.namedPath('cats', ...) for more flexibility |
ah thanks for bringing up the query bit. I think query params should work automatically. everything not included in the route definition is automatically a query param. I'd also rather pass a string for templates. |
So before anyone begins working on this, we should determine exactly the API wanted. |
I think this issue could be fixed with #1438 ? |
It’s been awhile, hopefully you guys find it useful. |
I'm submitting a...
Current behavior
I could not find a way to generate URLs to my configured routes. This is very useful in order to prevent hardcoding URLs all over templates and view code.
Expected behavior
There should be a function that receives a route alias and an optional list of params and returns the route URL.
The text was updated successfully, but these errors were encountered: