router icon indicating copy to clipboard operation
router copied to clipboard

Update Router.php

Open WeaponsTheyFear opened this issue 4 years ago • 3 comments

Added the option to return matching routes rather than directly executing callbacks.

// Create Router instance
$router = new Bramus\Router\Router();
$router->setReturnRoutes(true);

$router->before('GET', '/', function() {
    echo 'before middleware';
});

// Define routes
$router->get('/', function() {
    echo 'hello';
});

// Run it!
$matches = $router->run();

foreach ($matches as $route) {
    $route['fn']();
}

WeaponsTheyFear avatar Dec 19 '20 07:12 WeaponsTheyFear

Done!

WeaponsTheyFear avatar Jan 08 '21 22:01 WeaponsTheyFear

this is an awesome feature (I coud just use very well) - looking forward to the moment this gets merged into the master 👍 👍 👍

bassauer-storms-media avatar Feb 12 '21 19:02 bassauer-storms-media

FYI: tests are failing:

$ composer test
> ./vendor/bin/phpunit --colors=always

Deprecated: The each() function is deprecated. This message will be suppressed on further calls in /Users/bramus/repos/projects/php-router/vendor/phpunit/phpunit/src/Util/Getopt.php on line 38
PHPUnit 4.8.36 by Sebastian Bergmann and contributors.

........................FFF.........

Time: 47 ms, Memory: 4.00MB

There were 3 failures:

1) RouterTest::test404
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-'route not found'
+''

/Users/bramus/repos/projects/php-router/tests/RouterTest.php:641

2) RouterTest::test404WithClassAtMethod
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-'route not found'
+''

/Users/bramus/repos/projects/php-router/tests/RouterTest.php:667

3) RouterTest::test404WithClassAtStaticMethod
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-'route not found'
+''

/Users/bramus/repos/projects/php-router/tests/RouterTest.php:693

FAILURES!
Tests: 36, Assertions: 63, Failures: 3.
Script ./vendor/bin/phpunit --colors=always handling the test event returned with error code 1

It looks like there is an unwanted side-effect when it comes to handling 404's.

Also spotted a lost $numHandled on line 311.

bramus avatar Feb 14 '21 21:02 bramus