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

Named routes #3743

Closed
cojack opened this issue Jan 3, 2020 · 14 comments · May be fixed by #5117
Closed

Named routes #3743

cojack opened this issue Jan 3, 2020 · 14 comments · May be fixed by #5117

Comments

@cojack
Copy link
Contributor

cojack commented Jan 3, 2020

Feature Request

Is your feature request related to a problem? Please describe.

So, the problem it self comes when you're build a view, paths can change,you can switch global prefix, or whatever but router name, will be static, stick to the dynamic path.

Describe the solution you'd like

So I would like to have a possibility to set a name to the route that I can use it in view.

Teachability, Documentation, Adoption, Migration Strategy

I general, there are 2 packages, that some1 might think they cover the problem, first one is: https://www.npmjs.com/package/named-routes which do what it should do, but only on the Router scope, which is useless in our case, because each of the controller have his own isolated router, so you don't know the full path, second one is https://www.npmjs.com/package/express-reverse which seems to do the full funny staff for us, but idk if if it's not working the same way as the previous one.

So right now that we have:

@Controller('admin')
export class Admin {
  @Get('/')
  public indexAction() {
    // ... logic
  }
}

I would like to propose to add a second optional parameter to METHOD decorators

that might looks like:

@Controller('admin')
export class Admin {
  @Get('/', 'admin_index') // where string will be a default name for route
  // or
  @Get('/', {name: 'admin_index'})
  public indexAction() {
    // ... logic
  }
}

and then in view, you will be able to use kind of function to get back the full url, fe:

<a href="{{ url('admin_index') }}">Admin Panel</a>

Where url function|helper might live in express.local if we're talking about express or in different location, regarding to adapter.

What is the motivation / use case for changing the behavior?

Problem here lives between a developer and web designer co we can call it PEKBAC in general, but I would like to solve it in different approach, kind of contracts approach, like when a developer who wrote a whole business logic make a deal with a web developer who build a views, that routes will be named in some way, web developer doesn't have to think about prefiex, or care about developer changes in paths. Even if you have in plenty places link to specific view, and you change its path, but you're using name path, until it won't change, you're safe.

Regards.

UPDATE

I have update a decorator proposition to handle it as well as with literal object notation, because maybe in future some1 might get other proposition, so this will allow to extend this variation.

@cojack cojack added needs triage This issue has not been looked into type: enhancement 🐺 labels Jan 3, 2020
@Enkosz
Copy link

Enkosz commented Jan 22, 2020

Interesting feature! I'm taking a look at the source and i have already done the decorator part, seems really good!

@ttsirkia
Copy link

This quite the same as in Django: https://docs.djangoproject.com/en/3.0/ref/templates/builtins/#url It also allows injecting parameters to the URL. Would be very helpful in MVC projects with templates to be rendered.

@RobertAKARobin
Copy link

A little surprised this feature doesn't exist, since it's fairly standard with other frameworks.

@kierans
Copy link

kierans commented Mar 2, 2020

Sails.js has getUrlFor.

This enhancement is a great idea. Looking forward to seeing it get merged. 👍

@kamilmysliwiec
Copy link
Member

I like this idea. However, I don't think that we should use @Get() (or any other HTTP related decorator) for this. Instead, we should provide a separate decorator that allow specifying the name of a route. Would you like to create a PR for this @cojack?

@kamilmysliwiec kamilmysliwiec added scope: common scope: core and removed needs triage This issue has not been looked into labels Mar 10, 2020
@getspooky
Copy link
Contributor

This enhancement is a great idea . I suggest to add a new decorator like WithName in order to avoid breaking changes.
carbon

@kamilmysliwiec
Copy link
Member

PRs are more than welcome :)

sjones6 added a commit to sjones6/nest that referenced this issue Jul 19, 2020
Add @WithAlias decorator which  allows arbitrary aliases
to be attached to Controller methods and later retrieved
in views and resolved to full route path.

Resolves nestjs#3743
sjones6 added a commit to sjones6/nest that referenced this issue Jul 19, 2020
Add @WithAlias decorator which  allows arbitrary aliases
to be attached to Controller methods and later retrieved
in views and resolved to full route path.

Resolves nestjs#3743
sjones6 added a commit to sjones6/nest that referenced this issue Jul 19, 2020
Add @WithAlias decorator which  allows arbitrary aliases
to be attached to Controller methods and later retrieved
in views and resolved to full route path.

Resolves nestjs#3743
@sjones6
Copy link

sjones6 commented Jul 19, 2020

@kamilmysliwiec , I've raised PR #5117 to add a @WithAlias decorator in line with the discussion here.

Looking for some feedback on the implementation as well as the method name exposed to views (currently getUrl).

Follows @getspooky 's idea, but uses @WithAlias for the decorator, rather than @WithName, as a route alias felt clearer in intention. But again, open to alternatives.

(It's my first contribution to nest, so fully expecting feedback to address ... great framework 💯 )

@hgezim
Copy link

hgezim commented Nov 18, 2020

Here's my use case: I want to disallow all requests except for a few API endpoints. @sjones6

@kamilmysliwiec
Copy link
Member

Let's track this here nestjs/docs.nestjs.com#1400

@ahmednawazkhan
Copy link

ahmednawazkhan commented Oct 24, 2021

This can also help in avoiding repetition in url writing in e2e tests. instead of rewriting the whole url, we can simply refer to named routed and passing necessary parameters.

@AeroNotix
Copy link

nestjs/docs.nestjs.com#1400 seems to maybe have stalled? Did this feature make it in through any other PRs by any chance

@zetti-caletti
Copy link

Look here, I made a Draft PR: #10178

@jnm733
Copy link

jnm733 commented Mar 3, 2023

This would be a great feature! I miss it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.