Skip to content

Commit 22a713d

Browse files
author
Milad Rahimi
committed
add url method
1 parent a694f0e commit 22a713d

File tree

1 file changed

+38
-15
lines changed

1 file changed

+38
-15
lines changed

src/Router.php

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -60,17 +60,17 @@ class Router
6060
/**
6161
* @var array
6262
*/
63-
private $currentMiddleware = [];
63+
private $groupMiddleware = [];
6464

6565
/**
6666
* @var string
6767
*/
68-
private $currentPrefix = '';
68+
private $groupPrefix = '';
6969

7070
/**
7171
* @var string|null
7272
*/
73-
private $currentDomain = null;
73+
private $groupDomain = null;
7474

7575
/**
7676
* @var array|null
@@ -104,9 +104,9 @@ public function group(array $attributes, Closure $routes)
104104
{
105105
// Backup previous group properties
106106
$oldName = $this->currentName;
107-
$oldMiddleware = $this->currentMiddleware;
108-
$oldPrefix = $this->currentPrefix;
109-
$oldDomain = $this->currentDomain;
107+
$oldMiddleware = $this->groupMiddleware;
108+
$oldPrefix = $this->groupPrefix;
109+
$oldDomain = $this->groupDomain;
110110

111111
// Empty current name for next steps
112112
$this->currentName = null;
@@ -117,27 +117,27 @@ public function group(array $attributes, Closure $routes)
117117
$attributes[RouteAttributes::MIDDLEWARE] = [$attributes[RouteAttributes::MIDDLEWARE]];
118118
}
119119

120-
$this->currentMiddleware = array_merge($attributes[RouteAttributes::MIDDLEWARE], $this->currentMiddleware);
120+
$this->groupMiddleware = array_merge($attributes[RouteAttributes::MIDDLEWARE], $this->groupMiddleware);
121121
}
122122

123123
// Set given prefix for the group
124124
if (isset($attributes[RouteAttributes::PREFIX])) {
125-
$this->currentPrefix = $attributes[RouteAttributes::PREFIX] . $this->currentPrefix;
125+
$this->groupPrefix = $attributes[RouteAttributes::PREFIX] . $this->groupPrefix;
126126
}
127127

128128
// Set given domain for the group
129129
if (isset($attributes[RouteAttributes::DOMAIN])) {
130-
$this->currentDomain = $attributes[RouteAttributes::DOMAIN];
130+
$this->groupDomain = $attributes[RouteAttributes::DOMAIN];
131131
}
132132

133133
// Run group body closure
134134
call_user_func($routes, $this);
135135

136136
// Revert to previous group properties using the backups
137137
$this->currentName = $oldName;
138-
$this->currentDomain = $oldDomain;
139-
$this->currentPrefix = $oldPrefix;
140-
$this->currentMiddleware = $oldMiddleware;
138+
$this->groupDomain = $oldDomain;
139+
$this->groupPrefix = $oldPrefix;
140+
$this->groupMiddleware = $oldMiddleware;
141141
}
142142

143143
/**
@@ -158,16 +158,16 @@ public function map(
158158
string $domain = null,
159159
string $name = null
160160
) {
161-
$route = $this->currentPrefix . $route;
161+
$route = $this->groupPrefix . $route;
162162
$middleware = is_array($middleware) ? $middleware : [$middleware];
163163

164164
// Add the route to route list
165165
$this->routes[strtoupper($method)][$route] = [
166166
RouteAttributes::METHOD => $method,
167167
RouteAttributes::URI => $route,
168168
RouteAttributes::CONTROLLER => $controller,
169-
RouteAttributes::MIDDLEWARE => array_merge($this->currentMiddleware, $middleware),
170-
RouteAttributes::DOMAIN => $domain ?: $this->currentDomain,
169+
RouteAttributes::MIDDLEWARE => array_merge($this->groupMiddleware, $middleware),
170+
RouteAttributes::DOMAIN => $domain ?: $this->groupDomain,
171171
];
172172

173173
// Add the route to named route list
@@ -636,6 +636,29 @@ public function currentRouteName()
636636
return null;
637637
}
638638

639+
/**
640+
* generate URL for given route name
641+
*
642+
* @param string $routeName
643+
* @param array $parameters
644+
* @return string
645+
* @throws RouteNotFoundException
646+
*/
647+
public function url(string $routeName, array $parameters = []): string
648+
{
649+
if (isset($this->routeNames[$routeName]) == false) {
650+
throw new RouteNotFoundException();
651+
}
652+
653+
$uri = $this->routeNames[$routeName];
654+
655+
foreach ($parameters as $parameter) {
656+
$uri = preg_replace('/\??\{' . $parameter . '\??\}', $parameter, $uri);
657+
}
658+
659+
return $uri;
660+
}
661+
639662
/**
640663
* Publish http response manually
641664
*

0 commit comments

Comments
 (0)