/** * Add GET|POST|PUT|DELETE route * * Adds a new route to the router with associated callable. This * route will only be invoked when the HTTP request's method matches * this route's method. * * ARGUMENTS: * * First: string The URL pattern (REQUIRED) * In-Between: mixed Anything that returns TRUE for `is_callable` (OPTIONAL) * Last: mixed Anything that returns TRUE for `is_callable` (REQUIRED) * * The first argument is required and must always be the * route pattern (ie. '/books/:id'). * * The last argument is required and must always be the callable object * to be invoked when the route matches an HTTP request. * * You may also provide an unlimited number of in-between arguments; * each interior argument must be callable and will be invoked in the * order specified before the route's callable is invoked. * * USAGE: * * Slim::get('/foo'[, middleware, middleware, ...], callable); * * @param array (See notes above) * @return \Slim\Route */ protected function mapRoute($args) { $pattern = array_shift($args); $callable = array_pop($args); $route = $this->router->map($pattern, $callable); if (count($args) > 0) { $route->setMiddleware($args); } return $route; }
/** * @param Route $route */ public function addRoute(Route $route) { $slimRoute = new \Slim\Route($route->getPath(), [$this, 'dummyCallable']); $slimRoute->setName($route->getName()); $allowedMethods = $route->getAllowedMethods(); $slimRoute->via($allowedMethods === Route::HTTP_METHOD_ANY ? 'ANY' : $allowedMethods); // Process options $options = $route->getOptions(); if (isset($options['conditions']) && is_array($options['conditions'])) { $slimRoute->setConditions($options['conditions']); } // The middleware is merged with the rest of the route params $params = ['middleware' => $route->getMiddleware()]; if (isset($options['defaults']) && is_array($options['defaults'])) { $params = array_merge($options['defaults'], $params); } $slimRoute->setParams($params); $this->router->map($slimRoute); }
/** * @expectedException \RuntimeException */ public function testPathForRouteNotExists() { $methods = ['GET']; $pattern = '/hello/{first}/{last}'; $callable = function ($request, $response, $args) { echo sprintf('Hello %s %s', $args['first'], $args['last']); }; $route = $this->router->map($methods, $pattern, $callable); $route->setName('foo'); $this->router->pathFor('bar', ['first' => 'josh', 'last' => 'lockhart']); }