public function setUpRoutes() { $this->app->get('/', function (Request $request, Response $response, array $args) { $response->getBody()->write(file_get_contents(AppConfig::getPathToPublic() . '/app.html')); }); $this->app->any('/api/users/[{id}]', Controller\UsersController::class); $this->app->any('/api/sessions/[{api_key}]', Controller\SessionsController::class); $this->app->any('/api/trips/[{id}]', Controller\TripsController::class); $this->app->any('/api/trips/user/[{user_id}]', Controller\TripsController::class); }
public function testAnyRoute() { $path = '/foo'; $callable = function ($req, $res) { // Do something }; $app = new App(); $route = $app->any($path, $callable); $this->assertInstanceOf('\\Slim\\Route', $route); $this->assertAttributeContains('GET', 'methods', $route); $this->assertAttributeContains('POST', 'methods', $route); $this->assertAttributeContains('PUT', 'methods', $route); $this->assertAttributeContains('PATCH', 'methods', $route); $this->assertAttributeContains('DELETE', 'methods', $route); $this->assertAttributeContains('OPTIONS', 'methods', $route); }
/** * Sets main dynamic routing params into Slim app * * @param \Slim\App $app * * @return void */ public static function setMap($app, $namespace) { $app->any('/{controller}[/{params:.*}]', function ($request, $response, $args) use($app, $namespace) { $contDI = $app->getContainer(); // Get dependencies. // Get called method $method = strtolower($request->getMethod()); // Get config namespace if necessary $calledController = $namespace . '\\' . $args['controller']; $controller = new $calledController(); if (is_a($controller, "\\mbarquin\\SlimDR\\ControllerInterface") === false) { throw new \Exception('Controller must implement \\mbarquin\\SlimDR\\ControllerInterface'); } $funcArgs = \mbarquin\SlimDR\Factory::getArgs($request, $response, $args, $contDI, $controller, $method); call_user_func_array(array($controller, $method), $funcArgs); }); }