diff --git a/README.md b/README.md index 293b84f..018d4a3 100644 --- a/README.md +++ b/README.md @@ -1401,13 +1401,11 @@ Sometimes it can be useful to add a custom base path to all of the routes added. This can easily be done by taking advantage of the [Event Handlers](#events) support of the project. ```php -$basePath = '/basepath/'; +$basePath = '/basepath'; $eventHandler = new EventHandler(); $eventHandler->register(EventHandler::EVENT_ADD_ROUTE, function(EventArgument $event) use($basePath) { - // Make sure url is alway correct - $basePath = rtrim($basePath, '/'); $route = $event->route; // Skip routes added by group as these will inherit the url @@ -1417,18 +1415,16 @@ $eventHandler->register(EventHandler::EVENT_ADD_ROUTE, function(EventArgument $e switch (true) { case $route instanceof ILoadableRoute: - $route->setUrl($basePath . $route->getUrl()); + $route->prependUrl($basePath); break; case $route instanceof IGroupRoute: - $route->setPrefix($basePath . $route->getPrefix()); + $route->prependPrefix($basePath); break; } }); -$results = []; - TestRouter::addEventHandler($eventHandler); ``` @@ -1722,40 +1718,17 @@ You can read more about adding your own custom regular expression for matching p ### Multiple routes matches? Which one has the priority? -The router will match routes in the order they're added. +The router will match routes in the order they're added and will render multiple routes, if they match. -It's possible to render multiple routes. - -If you want the router to stop when a route is matched, you simply return a value in your callback or stop the execution manually (using `response()->json()` etc.). +If you want the router to stop when a route is matched, you simply return a value in your callback or stop the execution manually (using `response()->json()` etc.) or simply by returning a result. Any returned objects that implements the `__toString()` magic method will also prevent other routes from being rendered. +If you want the router only to execute one route per request, you can [disabling multiple route rendering](#disable-multiple-route-rendering). + ### Using the router on sub-paths -Using the library on a sub-path like `localhost/project/` is not officially supported, however it is possible to get it working quite easily. - -Add an event that appends your sub-path when a new loadable route is added. - -**Example:** - -```php -// ... your routes.php file - -if($isRunningLocally) { - - $eventHandler = new EventHandler(); - $eventHandler->register(EventHandler::EVENT_ADD_ROUTE, function (EventArgument $arg) use (&$status) { - - if ($arg->route instanceof \Pecee\SimpleRouter\Route\LoadableRoute) { - $arg->route->prependUrl('/local-path'); - } - - }); - - TestRouter::addEventHandler($eventHandler); - -} -``` +Please refer to [Setting custom base path](#setting-custom-base-path) part of the documentation. ## Debugging diff --git a/src/Pecee/SimpleRouter/Route/IGroupRoute.php b/src/Pecee/SimpleRouter/Route/IGroupRoute.php index ff26273..1f41c27 100644 --- a/src/Pecee/SimpleRouter/Route/IGroupRoute.php +++ b/src/Pecee/SimpleRouter/Route/IGroupRoute.php @@ -53,6 +53,14 @@ interface IGroupRoute extends IRoute */ public function setDomains(array $domains): self; + /** + * Prepend url + * + * @param string $url + * @return static + */ + public function prependUrl(string $url): self; + /** * Set prefix that child-routes will inherit. * diff --git a/src/Pecee/SimpleRouter/Route/RouteGroup.php b/src/Pecee/SimpleRouter/Route/RouteGroup.php index d605091..714a9a9 100644 --- a/src/Pecee/SimpleRouter/Route/RouteGroup.php +++ b/src/Pecee/SimpleRouter/Route/RouteGroup.php @@ -153,6 +153,17 @@ class RouteGroup extends Route implements IGroupRoute return $this; } + /** + * Prepend prefix + * + * @param string $url + * @return static + */ + public function prependUrl(string $url): IGroupRoute + { + return $this->setPrefix(rtrim($url, '/') . $this->prefix); + } + /** * Set prefix that child-routes will inherit. * diff --git a/tests/Pecee/SimpleRouter/EventHandlerTest.php b/tests/Pecee/SimpleRouter/EventHandlerTest.php index 4d73cc0..ec170c8 100644 --- a/tests/Pecee/SimpleRouter/EventHandlerTest.php +++ b/tests/Pecee/SimpleRouter/EventHandlerTest.php @@ -118,7 +118,7 @@ class EventHandlerTest extends \PHPUnit\Framework\TestCase switch (true) { case $data->route instanceof \Pecee\SimpleRouter\Route\ILoadableRoute: - $data->route->setUrl($basePath . $data->route->getUrl()); + $data->route->prependUrl($basePath); break; case $data->route instanceof \Pecee\SimpleRouter\Route\IGroupRoute: $data->route->setPrefix($basePath . $data->route->getPrefix());