DASPRiD / Dash

Flexible PSR-7 compliant HTTP router
BSD 2-Clause "Simplified" License
29 stars 9 forks source link

Allow to let routes decide if they can assemble #46

Closed bakura10 closed 9 years ago

bakura10 commented 10 years ago

ping @ocramius

Hi,

This PR tries to solve an issue I have with ZfrRest. With ZF2 router and current Dash, the only way to assemble a route is to know its name:

$path = $router->assemble([], ['name' => 'users']); // prints /users

However, routes are not always that simple. For instance, ZfrRest routes automatically traverses a hierarchy and can generate URI far beyond that what was defined in the "route" option. For instance, if a user has an association called "tweets", if you only define the "users" route with a "/users" path, ZfrRest may generate a lot of different URI: /users, /users/1, /users/2/tweets...

Because those routes are not actually defined in the config, there is currently no way to actually generate those URL without ugly hacks. What I'd like is actually letting my routes to decide if they can assemble something (based on parameters, options...). I could, for instance, use the router as follow:

$path = $router->assemble(['template' => '/users/{user_id}/tweets']);

The route could then decide to accept or reject the option. If the route returns true to "canAssemble", then the router let the given route to assemble.

Ocramius commented 10 years ago

I get what problem you are trying to solve is, but win which way is this PR a solution to it?

bakura10 commented 10 years ago

Well, you can create a route that implements this interface. If no route name is passed to the assemble method of the router, then the router iterate through all routes, and check if one can try to assemble a route BASED on the params.

DASPRiD commented 10 years ago

Why don't you implement this as a new path parser?

bakura10 commented 10 years ago

How would that help?

DASPRiD commented 9 years ago

I gonna decline it for now (especially since the code changed quite a bit since then).