Coder Social home page Coder Social logo

Comments (7)

philipobenito avatar philipobenito commented on June 23, 2024

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 [email protected]
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

from route.

thebigb avatar thebigb commented on June 23, 2024

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" [email protected] 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 [email protected]
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).

from route.

philipobenito avatar philipobenito commented on June 23, 2024

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 [email protected]
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" [email protected] 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 [email protected]
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)

from route.

thebigb avatar thebigb commented on June 23, 2024

Fair enough, I'll try that.

from route.

hannesvdvreken avatar hannesvdvreken commented on June 23, 2024

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

from route.

thebigb avatar thebigb commented on June 23, 2024

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.

from route.

hannesvdvreken avatar hannesvdvreken commented on June 23, 2024

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

from route.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.