From cb58b4975b20bf8116dedc27c823b5e03835c122 Mon Sep 17 00:00:00 2001 From: JoeBengalen Date: Sun, 16 Aug 2015 14:31:56 +0200 Subject: [PATCH] Added basePath support in Router for pathFor() --- Slim/Router.php | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/Slim/Router.php b/Slim/Router.php index c778215a6..12180b28b 100644 --- a/Slim/Router.php +++ b/Slim/Router.php @@ -37,6 +37,13 @@ class Router extends RouteCollector implements RouterInterface */ private $routeParser; + /** + * Base path used in pathFor() + * + * @var string + */ + protected $basePath = ''; + /** * Routes * @@ -74,6 +81,24 @@ public function __construct(RouteParser $parser = null, DataGenerator $generator $this->routeParser = $parser; } + /** + * Set the base path used in pathFor() + * + * @param string $basePath + * + * @return self + */ + public function setBasePath($basePath) + { + if (!is_string($basePath)) { + throw new InvalidArgumentException('Router basePath must be a string'); + } + + $this->basePath = $basePath; + + return $this; + } + /** * Add route * @@ -264,6 +289,10 @@ public function pathFor($name, array $data = [], array $queryParams = []) } $url = implode('', $segments); + if ($this->basePath) { + $url = $this->basePath . $url; + } + if ($queryParams) { $url .= '?' . http_build_query($queryParams); }