router
router copied to clipboard
Update Router.php
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']();
}
Done!
this is an awesome feature (I coud just use very well) - looking forward to the moment this gets merged into the master 👍 👍 👍
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.