Simplified RouteController and RouteResource by moving common group-match code to parent method.

This commit is contained in:
Simon Sessingø
2021-05-20 15:15:26 +02:00
parent 03ef9dfb74
commit eea30d0f59
3 changed files with 15 additions and 3 deletions
+13 -1
View File
@@ -100,6 +100,18 @@ abstract class LoadableRoute extends Route implements ILoadableRoute
return $this->url; return $this->url;
} }
/**
* Returns true if group is defined and matches the given url.
*
* @param string $url
* @param Request $request
* @return bool
*/
protected function matchGroup(string $url, Request $request): bool
{
return ($this->getGroup() === null || $this->getGroup()->matchRoute($url, $request) === true);
}
/** /**
* Find url that matches method, parameters or name. * Find url that matches method, parameters or name.
* Used when calling the url() helper. * Used when calling the url() helper.
@@ -203,9 +215,9 @@ abstract class LoadableRoute extends Route implements ILoadableRoute
* Sets the router name, which makes it easier to obtain the url or router at a later point. * Sets the router name, which makes it easier to obtain the url or router at a later point.
* Alias for LoadableRoute::setName(). * Alias for LoadableRoute::setName().
* *
* @see LoadableRoute::setName()
* @param string|array $name * @param string|array $name
* @return static * @return static
* @see LoadableRoute::setName()
*/ */
public function name($name): ILoadableRoute public function name($name): ILoadableRoute
{ {
@@ -88,7 +88,7 @@ class RouteController extends LoadableRoute implements IControllerRoute
public function matchRoute(string $url, Request $request): bool public function matchRoute(string $url, Request $request): bool
{ {
if ($this->getGroup() !== null && $this->getGroup()->matchRoute($url, $request) === false) { if ($this->matchGroup($url, $request) === false) {
return false; return false;
} }
@@ -85,7 +85,7 @@ class RouteResource extends LoadableRoute implements IControllerRoute
public function matchRoute(string $url, Request $request): bool public function matchRoute(string $url, Request $request): bool
{ {
if ($this->getGroup() !== null && $this->getGroup()->matchRoute($url, $request) === false) { if ($this->matchGroup($url, $request) === false) {
return false; return false;
} }