public function testAddCallableConvertsToRoute() { // Create our collection with NO data $routes = new RouteCollection(); $callable = function () { }; // Add our data $routes->add($callable); $this->assertNotSame($callable, current($routes->all())); $this->assertTrue(current($routes->all()) instanceof Route); }
/** * Add a new route to be matched on dispatch * * Essentially, this method is a standard "Route" builder/factory, * allowing a loose argument format and a standard way of creating * Route instances * * This method takes its arguments in a very loose format * The only "required" parameter is the callback (which is very strange considering the argument definition order) * * <code> * $router = new Router(); * * $router->respond( function() { * echo 'this works'; * }); * $router->respond( '/endpoint', function() { * echo 'this also works'; * }); * $router->respond( 'POST', '/endpoint', function() { * echo 'this also works!!!!'; * }); * </code> * * @param string | array $method HTTP Method to match * @param string $path Route URI path to match * @param callable $callback Callable callback method to execute on route match * @param bool $useNamespace * @return Route * todo: namespace should be OOP and extendable/customizable, while keeping the simplicity * of the current automated namespace system */ public function respond($method, $path = '*', $callback = null, $useNamespace = self::ROUTE_NAMESPACE) { // Get the arguments in a very loose format $arguments = func_get_args(); if (isset($arguments[3])) { unset($arguments[3]); } extract($this->parseLooseArgumentOrder($arguments), EXTR_OVERWRITE); $route = $this->route_factory->build($callback, $path, $method, true, null, $useNamespace); $this->routes->add($route); return $route; }