private function setSlimRules($rules) { foreach ($rules as $controllers) { if (isset($controllers['groups'])) { $this->slim->group($controllers['pattern'], function () use($controllers) { foreach ($controllers['groups'] as $controller) { $this->map($controller['methods'], $controller['pattern'], $controller['action'])->setName($controller['name']); } }); } else { $this->slim->map($controllers['methods'], $controllers['pattern'], $controllers['action'])->setName($controllers['name']); } } }
protected function setRouteForPostTypes($postType, $defaultController) { $controllerPageMapping = $this->app->getContainer()->get(ControllerPageMappingField::class); /** * @FIXME: In future versions, need to change * adding routes to the map of get|post. All WP PAGES and POSTS must * coresponds to only GET method. Because it is has content only for reading. * All other logic like writing or another logic should be implemented in WIDGETS * or in controllers via declarring new routes and handlers for them. */ foreach ($this->wpService->get_posts(['numberposts' => -1, 'post_type' => $postType]) as $post) { $controller = $controllerPageMapping->getValue($post->ID); $this->app->map(['get', 'post'], parse_url(get_permalink($post), PHP_URL_PATH), $this->app->getContainer()->get(empty($controller) ? $defaultController : $controller))->setArgument('requestedEntity', $post); } }
/** * Add the controller's routes and relative methods. * Request and response are PSR-7 compliant * @param App $application the Slim application to be configured * @return null */ public static function createRoutes(App $application) { // Note: in the SlimFramework's routing function closures, $this equals to the App's Container. $base = static::$baseUri; // Routes that need the user to be admin if (Utils::isAdmin()) { // Product creation (view) $application->map(['GET', 'POST'], "{$base}/create[/]", function (Request $request, Response $response) { return (new self($this))->handleCreate($request, $response); })->setName('productCreate'); // Products list (view) $application->get("{$base}/list[/]", function (Request $request, Response $response) { return (new self($this))->handleReadList($request, $response); })->setName('productList'); // Product edit (view) $application->map(['GET', 'POST'], "{$base}/{product}/edit[/]", function (Request $request, Response $response, $args) { return (new self($this))->handleEdit($request, $response, intval($args['product'])); })->setName('productEdit'); } // Routes that can be seen by anyone // ... }
public function map(array $methods, $pattern, $target) { if (is_string($target)) { parent::map($methods, $pattern, function () use($target) { return self::modules()->dispatcher()->dispatch($target, func_get_args()); }); } else { if (is_callable($target)) { parent::map($methods, $pattern, $target); } else { throw new InvalidTargetException(); } } }
/** * {@inheritdoc} */ public function &extend($path, array $method_to_action = [], $name = '', $controller_name = null) { if (empty($path)) { throw new InvalidArgumentException('Route path is required'); } $path = trim($path, '/'); if (empty($name)) { $name = str_replace(['{', '}', '/', '-'], ['', '', '_', '_'], $path); } if (empty($method_to_action)) { $method_to_action = ['GET' => $name]; } if (empty($controller_name)) { $controller = $this->settings['controller']; } else { $controller = strpos($controller_name, '\\') === false ? $this->controller_namespace . '\\' . $controller_name : $controller_name; } $route = $this->app->map(array_keys($method_to_action), "{$this->settings['path']}/{$path}", $controller)->setName("{$this->settings['name']}_{$name}"); foreach ($method_to_action as $method => $action) { $route->setArgument("{$method}_action", $action); } return $route; }
private function getSlimInstance() { if (!$this->slim) { $testCase = new WebTestCase(); $this->slim = $testCase->getSlimInstance(); $methods = $this->getValidRequestMethods(); $callback = function ($req, $res) { return $res->write('This is a test!'); }; foreach ($methods as $method) { $this->slim->map([$method], $this->getValidUri(), $callback); } } return $this->slim; }
/** * {@inheritdoc} */ public function mapModel($model_class, array $settings = null, callable $extend_collection = null, callable $extend_single = null) { if (empty($model_class)) { throw new InvalidArgumentException('Model class is required'); } if (empty($settings)) { $settings = []; } if (empty($settings['model_name'])) { $model_name = $this->getModelNameFromClass($model_class); } else { $model_name = $settings['model_name']; } $controller = empty($settings['controller']) ? $this->controller_namespace . '\\' . Inflector::classify($model_name) . 'Controller' : $settings['controller']; $id = empty($settings['id']) ? Inflector::singularize($model_name) . '_id' : $settings['id']; $id_format = empty($settings['id_format']) ? '[0-9]+' : $settings['id_format']; if (empty($settings['collection_path'])) { $collection_path = '/' . str_replace('_', '-', $model_name); } else { $collection_path = '/' . ltrim($settings['collection_path'], '/'); } if (empty($settings['single_path'])) { $single_path = $collection_path . '/{' . $id . ':' . $id_format . '}'; } else { $single_path = '/' . ltrim($settings['single_path'], '/'); } $collection_settings = ['name' => $model_name, 'path' => $collection_path, 'controller' => $controller]; $single_settings = ['name' => Inflector::singularize($collection_settings['name']), 'path' => $single_path, 'controller' => $controller]; $this->app->map(['GET', 'POST'], $collection_settings['path'], $collection_settings['controller'])->setName($collection_settings['name'])->setArgument('GET_action', 'index')->setArgument('POST_action', 'add'); $this->app->map(['GET', 'PUT', 'DELETE'], $single_settings['path'], $single_settings['controller'])->setName($single_settings['name'])->setArgument('GET_action', 'view')->setArgument('PUT_action', 'edit')->setArgument('DELETE_action', 'delete'); if ($extend_collection) { call_user_func($extend_collection, new RouteExtender($this->app, $this->controller_namespace, $collection_settings)); } if ($extend_single) { call_user_func($extend_single, new RouteExtender($this->app, $this->controller_namespace, $single_settings)); } }
public function testMapRoute() { $path = '/foo'; $callable = function ($req, $res) { // Do something }; $app = new App(); $route = $app->map(['GET', 'POST'], $path, $callable); $this->assertInstanceOf('\\Slim\\Route', $route); $this->assertAttributeContains('GET', 'methods', $route); $this->assertAttributeContains('POST', 'methods', $route); }
/** * Load a route into the application. * * @param App $slim * @param string $name * @param array $details * * @return Route */ private function loadRoute(App $slim, $name, array $details) { $methods = $this->methods($details); $pattern = $this->nullable('route', $details); $stack = $details['stack']; $controller = array_pop($stack); $route = $slim->map($methods, $pattern, $controller); $route->setName($name); while ($middleware = array_pop($stack)) { $route->add($middleware); } return $route; }
public function addRoute(Route $route, \Closure $controllerClosure) { $this->slimApp->map([$route->httpVerb()], $route->pathExpression(), $controllerClosure)->setName($route->name()); }
/** * Register routes onto the app * @param App $app */ public static function define(App $app) { $app->map(['GET'], '/', HelloController::class . ':view'); }