DASPRiD / Dash

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

Syntax for prepending and appending routes #37

Closed bakura10 closed 10 years ago

bakura10 commented 10 years ago

Hi everyone,

In ZF2, I am always confused about how I can prepend or append routes to routes that were defined by a third-party module. For instance, let's say a module provides the following route:

return [
    'oauth' => ['/oauth', 'controller', 'action', 'children' => []]
];

Now, if I want to prepend each of those routes with a custom route (for instance an API base route), I'd suggest adding a "prepend" parameter:

return [
    'oauth' => ['/oauth', 'controller', 'action', 'prepend' => 'api', 'children' => []]
];

The same can occur to append routes using the "append" keyword. Of course, when a prepend or append is added, the old route is not accessible anymore through its origin URL (so for instance oauth route is no longer accessible from "/oauth" but only from "/api/oauth".

Ping @ocramius for syntax

Ocramius commented 10 years ago

Seems even more confusing to me, especially the bit about

oauth route is no longer accessible from "/oauth" but only from "/api/oauth"

That can really be annoying to debug.

DASPRiD commented 10 years ago

I already told him on IRC than he can just override the path if he wants. Thus closing.

bakura10 commented 10 years ago

Just to make 100% sure Dash support my use-case:

Example:

My ZfrOAuth2 module provides a route to create a token. Using Dash notation, it would be something like:

return [
    'router' => [
        'routes' => [
        'oauth-token' => ['/oauth-token', 'token', TokenController::class]
    ]
    ]
];

Now, for my app, I want this route to be accessible only with the "connect." hostname (connect.mysite.com/oauth-token). Do we agree that I simply need to do that in my application config:

return [
    'router' => [
    'routes' => [
        'oauth-token' => [['hostname' => 'connect.mysite.com']]
    ]
    ]
];

Is Dash smart enough to realize that I pass an array as first param (and not the shortcuts), so I don't need to rewrite all the settings?

Now, let's say I want to force multiple routes to be only accessible using the connect hostname. I therefore define a new route, to avoid duplicated the "connect" hostname.

return [
    'router' => [
    'routes' => [
        'base-connect' => [['hostname' => 'connect.mysite.com']]
    ]
    ]
];

I think we really need a "prepend_by" parameter, so I can replace my oauth route to:

return [
    'router' => [
    'routes' => [
        'oauth-token' => [['prepend_by' => 'base-connect']]
    ]
    ]
];

Now, I can act globally on the "base-connect" route, without having to change all the routes that need to be prepended. A typical use case for this is environment specific route (you will have "connect.mysite.com" for prod, but "connect.mysite.localhost" for dev for instance).

I can come up with an implementation if you want.

bakura10 commented 10 years ago

Note: this is quite similar in approach to the various "prototypes" that ZF2 has (but that are REALLY unclear to use). "prepend_by" is much much better.