Skip to content
This repository was archived by the owner on Oct 11, 2024. It is now read-only.

Commit dc37447

Browse files
authored
Merge pull request #9 from crosa7/add-support-for-named-params-in-route-method
Add support for named params in route() method
2 parents d7d66f7 + 1540c02 commit dc37447

2 files changed

Lines changed: 59 additions & 17 deletions

File tree

src/Router.php

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* Leaf Router
1111
* ---------------
1212
* Super simple and powerful routing with Leaf
13-
*
13+
*
1414
* @author Michael Darko
1515
* @since 1.2.0
1616
* @version 3.0
@@ -83,7 +83,7 @@ public static function mount(string $path, $handler)
8383

8484
/**
8585
* Alias for mount
86-
*
86+
*
8787
* @param string $path The route sub pattern/path to mount the callbacks on
8888
* @param callable|array $handler The callback method
8989
*/
@@ -96,7 +96,7 @@ public static function group(string $path, $handler)
9696

9797
/**
9898
* Store a route and it's handler
99-
*
99+
*
100100
* @param string $methods Allowed HTTP methods (separated by `|`)
101101
* @param string $pattern The route pattern/path to match
102102
* @param string|array|callable $handler The handler for route when matched
@@ -156,7 +156,7 @@ public static function match(string $methods, string $pattern, $handler)
156156

157157
/**
158158
* Add a route with all available HTTP methods
159-
*
159+
*
160160
* @param string $pattern The route pattern/path to match
161161
* @param string|array|callable The handler for route when matched
162162
*/
@@ -171,7 +171,7 @@ public static function all(string $pattern, $handler)
171171

172172
/**
173173
* Add a route with GET method
174-
*
174+
*
175175
* @param string $pattern The route pattern/path to match
176176
* @param string|array|callable The handler for route when matched
177177
*/
@@ -182,7 +182,7 @@ public static function get(string $pattern, $handler)
182182

183183
/**
184184
* Add a route with POST method
185-
*
185+
*
186186
* @param string $pattern The route pattern/path to match
187187
* @param string|array|callable The handler for route when matched
188188
*/
@@ -193,7 +193,7 @@ public static function post(string $pattern, $handler)
193193

194194
/**
195195
* Add a route with PUT method
196-
*
196+
*
197197
* @param string $pattern The route pattern/path to match
198198
* @param string|array|callable The handler for route when matched
199199
*/
@@ -204,7 +204,7 @@ public static function put(string $pattern, $handler)
204204

205205
/**
206206
* Add a route with PATCH method
207-
*
207+
*
208208
* @param string $pattern The route pattern/path to match
209209
* @param string|array|callable The handler for route when matched
210210
*/
@@ -215,7 +215,7 @@ public static function patch(string $pattern, $handler)
215215

216216
/**
217217
* Add a route with OPTIONS method
218-
*
218+
*
219219
* @param string $pattern The route pattern/path to match
220220
* @param string|array|callable The handler for route when matched
221221
*/
@@ -226,7 +226,7 @@ public static function options(string $pattern, $handler)
226226

227227
/**
228228
* Add a route with DELETE method
229-
*
229+
*
230230
* @param string $pattern The route pattern/path to match
231231
* @param string|array|callable The handler for route when matched
232232
*/
@@ -254,7 +254,7 @@ public static function redirect(
254254

255255
/**
256256
* Create a resource route for using controllers.
257-
*
257+
*
258258
* This creates a routes that implement CRUD functionality in a controller
259259
* `/posts` creates:
260260
* - `/posts` - GET | HEAD - Controller@index
@@ -264,7 +264,7 @@ public static function redirect(
264264
* - `/posts/{id}/edit` - GET | HEAD - Controller@edit
265265
* - `/posts/{id}/edit` - POST | PUT | PATCH - Controller@update
266266
* - `/posts/{id}/delete` - POST | DELETE - Controller@destroy
267-
*
267+
*
268268
* @param string $pattern The base route to use eg: /post
269269
* @param string $controller to handle route eg: PostController
270270
*/
@@ -281,15 +281,15 @@ public static function resource(string $pattern, string $controller)
281281

282282
/**
283283
* Create a resource route for using controllers without the create and edit actions.
284-
*
284+
*
285285
* This creates a routes that implement CRUD functionality in a controller
286286
* `/posts` creates:
287287
* - `/posts` - GET | HEAD - Controller@index
288288
* - `/posts` - POST - Controller@store
289289
* - `/posts/{id}` - GET | HEAD - Controller@show
290290
* - `/posts/{id}/edit` - POST | PUT | PATCH - Controller@update
291291
* - `/posts/{id}/delete` - POST | DELETE - Controller@destroy
292-
*
292+
*
293293
* @param string $pattern The base route to use eg: /post
294294
* @param string $controller to handle route eg: PostController
295295
*/
@@ -304,7 +304,7 @@ public static function apiResource(string $pattern, string $controller)
304304

305305
/**
306306
* Redirect to another route
307-
*
307+
*
308308
* @param string|array $route The route to redirect to
309309
* @param array|null $data Data to pass to the next route
310310
*/
@@ -335,15 +335,31 @@ public static function push($route, ?array $data = null)
335335
* Get route url by defined route name
336336
*
337337
* @param string $routeName
338+
* @param array|string|null $params
338339
*
339340
* @return string
340341
*/
341-
public static function route(string $routeName): string
342+
public static function route(string $routeName, $params = null): string
342343
{
343344
if (!isset(static::$namedRoutes[$routeName])) {
344345
trigger_error('Route named ' . $routeName . ' not found');
345346
}
346347

347-
return static::$namedRoutes[$routeName];
348+
$routePath = static::$namedRoutes[$routeName];
349+
if ($params) {
350+
if (is_array($params)) {
351+
foreach ($params as $key => $value) {
352+
if (!preg_match('/{('. $key .')}/', $routePath)) {
353+
trigger_error('Param "' . $key . '" not found in route "' . static::$namedRoutes[$routeName] . '"');
354+
}
355+
$routePath = str_replace('{' . $key . '}', $value, $routePath);
356+
}
357+
}
358+
if (is_string($params)) {
359+
$routePath = preg_replace('/{(.*?)}/', $params, $routePath);
360+
}
361+
}
362+
363+
return $routePath;
348364
}
349365
}

tests/routes.test.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,34 @@ class TRoute
118118
expect($routeUrl)->toBe('/route/url');
119119
});
120120

121+
test('route should return url with replaced named params using params as array', function () {
122+
$router = new Router;
123+
$router->match('GET', '/movies/{movieId}/photos/{photoId}', ['handler', 'name' => 'route-name']);
124+
125+
$routeUrl = $router->route('route-name', ['movieId' => 'my-movie', 'photoId' => 'my-photo']);
126+
127+
expect($routeUrl)->toBe('/movies/my-movie/photos/my-photo');
128+
});
129+
130+
test('route should return url with replaced named params using params as string', function () {
131+
$router = new Router;
132+
$router->match('GET', '/movies/{movieId}/edit', ['handler', 'name' => 'route-name']);
133+
134+
$routeUrl = $router->route('route-name', 'my-movie');
135+
136+
expect($routeUrl)->toBe('/movies/my-movie/edit');
137+
});
138+
121139
test('route should throw exception if no route found for name', function () {
122140
$router = new Router;
123141

124142
expect(fn() => $router->route('non-existent-route-name'))->toThrow(Exception::class);
125143
});
144+
145+
test('route should throw error if no named param found for params array', function () {
146+
$router = new Router;
147+
$router->match('GET', '/movies/{movieId}/photos/{photoId}', ['handler', 'name' => 'route-name']);
148+
149+
expect(fn() => $router->route('route-name', ['movieId' => 'my-movie', 'otherId' => 'my-photo', 'someOtherId' => 'my-music']))
150+
->toThrow(Exception::class, 'Param "otherId" not found in route "/movies/{movieId}/photos/{photoId}"');
151+
});

0 commit comments

Comments
 (0)