Added prependPrefix to Group class & updated documentation.

This commit is contained in:
Simon Sessingø
2021-03-31 03:23:04 +02:00
parent 7adb4e8597
commit eccda10169
4 changed files with 28 additions and 36 deletions
+8 -35
View File
@@ -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. This can easily be done by taking advantage of the [Event Handlers](#events) support of the project.
```php ```php
$basePath = '/basepath/'; $basePath = '/basepath';
$eventHandler = new EventHandler(); $eventHandler = new EventHandler();
$eventHandler->register(EventHandler::EVENT_ADD_ROUTE, function(EventArgument $event) use($basePath) { $eventHandler->register(EventHandler::EVENT_ADD_ROUTE, function(EventArgument $event) use($basePath) {
// Make sure url is alway correct
$basePath = rtrim($basePath, '/');
$route = $event->route; $route = $event->route;
// Skip routes added by group as these will inherit the url // 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) { switch (true) {
case $route instanceof ILoadableRoute: case $route instanceof ILoadableRoute:
$route->setUrl($basePath . $route->getUrl()); $route->prependUrl($basePath);
break; break;
case $route instanceof IGroupRoute: case $route instanceof IGroupRoute:
$route->setPrefix($basePath . $route->getPrefix()); $route->prependPrefix($basePath);
break; break;
} }
}); });
$results = [];
TestRouter::addEventHandler($eventHandler); 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? ### 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.) or simply by returning a result.
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.).
Any returned objects that implements the `__toString()` magic method will also prevent other routes from being rendered. 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 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. Please refer to [Setting custom base path](#setting-custom-base-path) part of the documentation.
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);
}
```
## Debugging ## Debugging
@@ -53,6 +53,14 @@ interface IGroupRoute extends IRoute
*/ */
public function setDomains(array $domains): self; 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. * Set prefix that child-routes will inherit.
* *
@@ -153,6 +153,17 @@ class RouteGroup extends Route implements IGroupRoute
return $this; 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. * Set prefix that child-routes will inherit.
* *
@@ -118,7 +118,7 @@ class EventHandlerTest extends \PHPUnit\Framework\TestCase
switch (true) { switch (true) {
case $data->route instanceof \Pecee\SimpleRouter\Route\ILoadableRoute: case $data->route instanceof \Pecee\SimpleRouter\Route\ILoadableRoute:
$data->route->setUrl($basePath . $data->route->getUrl()); $data->route->prependUrl($basePath);
break; break;
case $data->route instanceof \Pecee\SimpleRouter\Route\IGroupRoute: case $data->route instanceof \Pecee\SimpleRouter\Route\IGroupRoute:
$data->route->setPrefix($basePath . $data->route->getPrefix()); $data->route->setPrefix($basePath . $data->route->getPrefix());