thephpleague / route

Fast PSR-7 based routing and dispatch component including PSR-15 middleware, built on top of FastRoute.
http://route.thephpleague.com
MIT License
651 stars 126 forks source link

There is no support for reverse routing? #260

Closed burzum closed 4 years ago

burzum commented 4 years ago

There is no support for reverse routing?

philipobenito commented 4 years ago

I'd need a little more context for what you're trying to achieve.

burzum commented 4 years ago

@philipobenito frameworks like Cake allow you to do reverse routing like

$this->Html->link('Foo Index', ['controller' => 'Foo', 'action' => 'index');
// /foo
$this->Html->link('View of a Record', ['controller' => 'Foo', 'action' => 'view', 'slug' => $slug);
// /foo/view/my-slug-var

to generate the route for it. Reversing the concept of routing a request, but resolving the URL by it's route params.

Some other routers allow you to build a route by using named routes like this one https://github.com/crysalead/router See https://github.com/crysalead/router#named-routes-and-reverse-routing

philipobenito commented 4 years ago

It's not really built in the same way as those routers, there is a way to set/get named routes and then you can infer a link from that, however, that will slow down the resolution of the route as it is then handled separately from un-named routes, which are just passed off to FastRoute.

$router->get('/example')->setName('example');

/** @var \League\Route\Route $exampleRoute */
$exampleRoute = $router->getNamedRoute('example');

To implement something fully to replicate the example above would slow down resolution of routes significantly at the moment so it's not something I'd consider adding right now.