Skip to content

Named routes #3743

@cojack

Description

@cojack

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions