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

Can't dispatch more than once on same RouteCollection #75

Closed
thebigb opened this issue Oct 8, 2015 · 7 comments
Closed

Can't dispatch more than once on same RouteCollection #75

thebigb opened this issue Oct 8, 2015 · 7 comments

Comments

@thebigb
Copy link

thebigb commented Oct 8, 2015

This is regarding the develop branch

When I try to dispatch more than once on the same RouteCollection instance, I get the following exception:

FastRoute\BadRouteException: Cannot register two routes matching "/" for method "GET" in /home/ubuntu/workspace/vendor/nikic/fast-route/src/DataGenerator/RegexBasedAbstract.php on line 55

If it's not intended to do so, this behaviour should probably be documented.

I can probably work around this with a factory of some sorts, but it would be neat if it was supported.

Minimal testcase:

use League\Route\RouteCollection;
use Zend\Diactoros\Response;
use Zend\Diactoros\ServerRequest;
use Zend\Diactoros\Uri;

$routes = new RouteCollection();
$routes->get('/', function() { return 'foo'; });

$routes->dispatch((new ServerRequest())->withUri(new Uri('/')), new Response());

$routes->dispatch((new ServerRequest())->withUri(new Uri('/')), new Response());
@philipobenito
Copy link
Member

It's not something that is intended. Once documentation is written then it will include a section on this.

What is your use case to dispatch twice?

On Thu, Oct 8, 2015 at 10:02 PM, Bernardo notifications@github.com
wrote:

When I try to dispatch more than once on the same RouteCollection instance, I get the following exception:

FastRoute\BadRouteException: Cannot register two routes matching "/" for method "GET" in /home/ubuntu/workspace/vendor/nikic/fast-route/src/DataGenerator/RegexBasedAbstract.php on line 55

If it's not intended to do so, this behaviour should probably be documented.
I can probably work around this with a factory of some sorts, but it would be neat if it was supported.
Minimal testcase:

use League\Route\RouteCollection;
use Zend\Diactoros\Response;
use Zend\Diactoros\ServerRequest;
use Zend\Diactoros\Uri;
$routes = new RouteCollection();
$routes->get('/', function() { return 'foo'; });
$routes->dispatch((new ServerRequest())->withUri(new Uri('/')), new Response());
$routes->dispatch((new ServerRequest())->withUri(new Uri('/')), new Response());

Reply to this email directly or view it on GitHub:
#75

@thebigb
Copy link
Author

thebigb commented Oct 8, 2015

My main use case at the moment is unit testing. I generate various requests
and run them against the same RouteCollection.

Another use case could be internal routing to achieve something like HMVC (
https://en.m.wikipedia.org/wiki/Hierarchical_model–view–controller).
On Oct 8, 2015 11:06 PM, "Phil Bennett" notifications@github.com wrote:

It's not something that is intended. Once documentation is written then it
will include a section on this.

What is your use case to dispatch twice?

On Thu, Oct 8, 2015 at 10:02 PM, Bernardo notifications@github.com
wrote:

When I try to dispatch more than once on the same RouteCollection
instance, I get the following exception:

FastRoute\BadRouteException: Cannot register two routes matching "/" for
method "GET" in
/home/ubuntu/workspace/vendor/nikic/fast-route/src/DataGenerator/RegexBasedAbstract.php
on line 55

If it's not intended to do so, this behaviour should probably be
documented.
I can probably work around this with a factory of some sorts, but it
would be neat if it was supported.
Minimal testcase:

use League\Route\RouteCollection;
use Zend\Diactoros\Response;
use Zend\Diactoros\ServerRequest;
use Zend\Diactoros\Uri;
$routes = new RouteCollection();
$routes->get('/', function() { return 'foo'; });
$routes->dispatch((new ServerRequest())->withUri(new Uri('/')), new
Response());
$routes->dispatch((new ServerRequest())->withUri(new Uri('/')), new
Response());

Reply to this email directly or view it on GitHub:
#75


Reply to this email directly or view it on GitHub
#75 (comment).

@philipobenito
Copy link
Member

Well the issue here is less the dispatch but the fact that the underlying router (FastRoute) does not allow matching twice so it's not something I can implement in the manner you describe.

When it comes to unit tests, my personal opinion is that if you're routing, then it's not a unit test really, unit test the controller callable and let your other test suites look after routing or trust that the router is unit tested to ensure your routes will be matched.

That being said, by using named routes you can achieve what you want to achieve by just pulling the route from the collection and dispatching the route directly. Handling it this way also has the benefit of not relying on paths, so they can change without having to then reactor where you're dispatching them from.

On Thu, Oct 8, 2015 at 10:21 PM, Bernardo notifications@github.com
wrote:

My main use case at the moment is unit testing. I generate various requests
and run them against the same RouteCollection.
Another use case could be internal routing to achieve something like HMVC (
https://en.m.wikipedia.org/wiki/Hierarchical_model–view–controller).
On Oct 8, 2015 11:06 PM, "Phil Bennett" notifications@github.com wrote:

It's not something that is intended. Once documentation is written then it
will include a section on this.

What is your use case to dispatch twice?

On Thu, Oct 8, 2015 at 10:02 PM, Bernardo notifications@github.com
wrote:

When I try to dispatch more than once on the same RouteCollection
instance, I get the following exception:

FastRoute\BadRouteException: Cannot register two routes matching "/" for
method "GET" in
/home/ubuntu/workspace/vendor/nikic/fast-route/src/DataGenerator/RegexBasedAbstract.php
on line 55

If it's not intended to do so, this behaviour should probably be
documented.
I can probably work around this with a factory of some sorts, but it
would be neat if it was supported.
Minimal testcase:

use League\Route\RouteCollection;
use Zend\Diactoros\Response;
use Zend\Diactoros\ServerRequest;
use Zend\Diactoros\Uri;
$routes = new RouteCollection();
$routes->get('/', function() { return 'foo'; });
$routes->dispatch((new ServerRequest())->withUri(new Uri('/')), new
Response());
$routes->dispatch((new ServerRequest())->withUri(new Uri('/')), new
Response());

Reply to this email directly or view it on GitHub:
#75


Reply to this email directly or view it on GitHub
#75 (comment).


Reply to this email directly or view it on GitHub:
#75 (comment)

@thebigb
Copy link
Author

thebigb commented Oct 9, 2015

Fair enough, I'll try that.

@thebigb thebigb closed this as completed Oct 9, 2015
@hannesvdvreken
Copy link
Contributor

Too bad it's because of fastroute. Would be nice to use league/route in a reactphp powered application or something, where the router object is reused for every request

@thebigb
Copy link
Author

thebigb commented Oct 9, 2015

You still can, but you'll have to build a wrapper around the dispatch method that creates a new RouteCollection and loads the routes for each dispatch.

@hannesvdvreken
Copy link
Contributor

Well, that's exactly what you would want to avoid :-)

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

No branches or pull requests

3 participants