Skip to content

Commit 5b2f23e

Browse files
update 'group' method
1 parent 5fc8749 commit 5b2f23e

File tree

2 files changed

+119
-38
lines changed

2 files changed

+119
-38
lines changed

src/RouteGroup.php

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Effectra\Router;
6+
7+
/**
8+
* Class RouteGroup
9+
* @package Effectra\Router
10+
*/
11+
class RouteGroup
12+
{
13+
/**
14+
* RouteGroup constructor.
15+
*
16+
* @param string $prePath The prefix path for routes in this group.
17+
* @param string $controller The controller class name.
18+
* @param Route $router The Route instance for defining routes.
19+
*/
20+
public function __construct(
21+
protected string $prePath,
22+
protected string $controller,
23+
protected Route $router
24+
) {
25+
}
26+
27+
/**
28+
* Define a GET route.
29+
*
30+
* @param string $pattern The route pattern.
31+
* @param string $method The controller method to call.
32+
* @return void
33+
*/
34+
public function get(string $pattern, string $method): void
35+
{
36+
$this->router->get($this->prePath . $pattern, [$this->controller, $method]);
37+
}
38+
39+
/**
40+
* Define a POST route.
41+
*
42+
* @param string $pattern The route pattern.
43+
* @param string $method The controller method to call.
44+
* @return void
45+
*/
46+
public function post(string $pattern, string $method): void
47+
{
48+
$this->router->post($this->prePath . $pattern, [$this->controller, $method]);
49+
}
50+
51+
/**
52+
* Define a PUT route.
53+
*
54+
* @param string $pattern The route pattern.
55+
* @param string $method The controller method to call.
56+
* @return void
57+
*/
58+
public function put(string $pattern, string $method): void
59+
{
60+
$this->router->put($this->prePath . $pattern, [$this->controller, $method]);
61+
}
62+
63+
/**
64+
* Define a DELETE route.
65+
*
66+
* @param string $pattern The route pattern.
67+
* @param string $method The controller method to call.
68+
* @return void
69+
*/
70+
public function delete(string $pattern, string $method): void
71+
{
72+
$this->router->delete($this->prePath . $pattern, [$this->controller, $method]);
73+
}
74+
75+
/**
76+
* Define a PATCH route.
77+
*
78+
* @param string $pattern The route pattern.
79+
* @param string $method The controller method to call.
80+
* @return void
81+
*/
82+
public function patch(string $pattern, string $method): void
83+
{
84+
$this->router->patch($this->prePath . $pattern, [$this->controller, $method]);
85+
}
86+
87+
/**
88+
* Define an OPTIONS route.
89+
*
90+
* @param string $pattern The route pattern.
91+
* @param string $method The controller method to call.
92+
* @return void
93+
*/
94+
public function options(string $pattern, string $method): void
95+
{
96+
$this->router->options($this->prePath . $pattern, [$this->controller, $method]);
97+
}
98+
99+
/**
100+
* Define a route that responds to any HTTP method.
101+
*
102+
* @param string $pattern The route pattern.
103+
* @param string $method The controller method to call.
104+
* @return void
105+
*/
106+
public function any(string $pattern, string $method): void
107+
{
108+
$this->router->any($this->prePath . $pattern, [$this->controller, $method]);
109+
}
110+
}

src/Utils.php

Lines changed: 9 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -48,50 +48,21 @@ public function auth(string $pattern, $controller): self
4848
}
4949
return $this;
5050
}
51+
5152
/**
52-
* Define a group of routes that share a common URL prefix.
53-
*example `['get|info/{id}' => 'info']`
53+
* Create a group of routes with a common prefix.
5454
*
55-
* @param string $common_route The common URL prefix for the group of routes.
56-
* @param mixed $controller The controller for the group of routes.
57-
* @param array $methods An array of HTTP methods and route patterns for the group of routes.
58-
* @return $this
59-
* @throws Exception if the route
60-
55+
* @param string $path The common prefix for routes in this group.
56+
* @param string|object $controller The controller class name or instance.
57+
* @param callable $routes A callable that defines routes within the group.
58+
* @return self Returns the current Route instance.
6159
*/
62-
public function group(string $common_route, $controller, array $methods): self
60+
public function group(string $path, $controller, callable $routes): self
6361
{
64-
$common_route = $this->remakeRoute($common_route);
65-
66-
foreach ($methods as $m) {
67-
68-
if (!is_array($m)) {
69-
throw new Exception("Only array passed, like ['get|info/{id}' => 'info'] ");
70-
}
71-
72-
if (!str_contains(key($m), '|')) {
73-
throw new Exception("Separator '|' not found");
74-
}
75-
76-
$ext_route = explode('|', key($m));
77-
78-
$action = $m[key($m)];
79-
80-
$method = trim($ext_route[0]);
81-
82-
if (!in_array($method, $this->methods)) {
83-
84-
throw new Exception("Http Method not exists !");
85-
}
86-
87-
$route = trim(end($ext_route));
88-
89-
if (method_exists($controller, $action)) {
90-
$this->$method($common_route . '/' . $route, [$controller, $action]);
91-
}
92-
}
62+
call_user_func_array($routes, [new RouteGroup($path, $controller, $this)]);
9363
return $this;
9464
}
65+
9566
/**
9667
9768
*Define a group of CRUD routes that share a common URL prefix.

0 commit comments

Comments
 (0)