Skip to content

Commit

Permalink
Added basePath support in Router for pathFor()
Browse files Browse the repository at this point in the history
  • Loading branch information
JoeBengalen committed Aug 16, 2015
1 parent 15aea44 commit cb58b49
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions Slim/Router.php
Expand Up @@ -37,6 +37,13 @@ class Router extends RouteCollector implements RouterInterface
*/
private $routeParser;

/**
* Base path used in pathFor()
*
* @var string
*/
protected $basePath = '';

/**
* Routes
*
Expand Down Expand Up @@ -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
*
Expand Down Expand Up @@ -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);
}
Expand Down

1 comment on commit cb58b49

@neilmillard
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Saves putting Base_path() in front of every thing. thank you

Please sign in to comment.