Skip to content

Commit

Permalink
fix : route group doesn't have middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
UnknownRori committed Feb 9, 2022
1 parent 21cf487 commit 457d10d
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 24 deletions.
2 changes: 1 addition & 1 deletion core/Support/http/route/IRoute.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public static function get($uri, $controller);
public static function post($uri, $controller);
public static function delete($uri, $controller);
public static function patch($uri, $controller);
public static function middleware($middleware);
public function middleware($middleware);
public static function GetRoute($name);
public static function Redirect($name);
public function Run($uri, $requestType);
Expand Down
76 changes: 54 additions & 22 deletions core/Support/http/route/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ class Route implements IRoute

private static $temp = [];

/**
* Destroy all the temporary
* @param void
*/
public function __destruct()
{
unset(self::$temp['uri']);
unset(self::$temp['method']);
}

/**
* Starting Point of Route class to Register URI to route
* Route::get|post|patch|delete('uri', [controller::class, 'method'])
Expand All @@ -40,11 +50,11 @@ public static function get($uri, $controller = [])
{
$self = new static;

self::setRoute('GET', $uri, $controller);

self::temp('uri', $uri);
self::temp('method', 'GET');

self::setRoute('GET', $uri, $controller);

return $self;
}

Expand All @@ -57,11 +67,11 @@ public static function post($uri, $controller = [])
{
$self = new static;

self::setRoute('POST', $uri, $controller);

self::temp('uri', $uri);
self::temp('method', 'POST');

self::setRoute('POST', $uri, $controller);

return $self;
}

Expand All @@ -75,11 +85,11 @@ public static function patch($uri, $controller = [])
{
$self = new static;

self::setRoute('PATCH', $uri, $controller);

self::temp('uri', $uri);
self::temp('method', 'PATCH');

self::setRoute('PATCH', $uri, $controller);

return $self;
}

Expand All @@ -92,22 +102,35 @@ public static function delete($uri, $controller = [])
{
$self = new static;

self::setRoute('DELETE', $uri, $controller);

self::temp('uri', $uri);
self::temp('method', 'DELETE');

self::setRoute('DELETE', $uri, $controller);

return $self;
}

/**
* Register multiple route into same middleware
* Starting point to register multiple routes into same middleware
* @param callable $callback
* @return void
* @return self
*/
public static function group($middleware)
{
$self = new static;
self::temp('middleware', $middleware);
return $self;
}

/**
* Register multiple routes into same middleware
*/
public static function group(callable $callback)
public function by(callable $callback)
{
self::temp('group_middleware', self::temp('middleware'));
call_user_func($callback);
unset(self::$temp['group_middleware']);
unset(self::$temp['middleware']);
}

/**
Expand All @@ -128,6 +151,9 @@ protected static function setRoute(string $method, string $uri, array|callable $
"action" => $controller[1],
];
}
if (self::temp('middleware') || self::temp('group_middleware')) {
self::setMiddleware();
}
}

/**
Expand Down Expand Up @@ -183,15 +209,15 @@ public static function Redirect($name, array $data = null)
* @param string $middleware MiddlewareName
* @return this
*/
public static function middleware($middleware)
public function middleware($middleware)
{
$self = new static;

self::temp('middleware', $middleware);

self::setMiddleware();
if (self::temp('uri')) {
self::setMiddleware();
}

return $self;
return $this;
}

/**
Expand All @@ -200,12 +226,18 @@ public static function middleware($middleware)
*/
protected static function setMiddleware()
{
self::$route[self::$temp['method']][self::$temp['uri']] = array_merge(
["middleware" => self::$temp['middleware']],
self::$route[self::$temp['method']][self::$temp['uri']]
);

unset(self::$temp['middleware']);
if (self::temp('group_middleware')) {
self::$route[self::$temp['method']][self::$temp['uri']] = array_merge(
["middleware" => self::$temp['group_middleware']],
self::$route[self::$temp['method']][self::$temp['uri']]
);
} else {
self::$route[self::$temp['method']][self::$temp['uri']] = array_merge(
["middleware" => self::$temp['middleware']],
self::$route[self::$temp['method']][self::$temp['uri']]
);
unset(self::$temp['middleware']);
}
}

/**
Expand Down
2 changes: 1 addition & 1 deletion core/docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ Route groups allow you to share route attributes, such as middleware across a la

To assign middleware to all route inside the group, you may used group method to define middleware and the route of the group

Route::middleware('test')->group(function () {
Route::group('test')->by(function () {
Route::get('/group/1', [Group::class, 'index']);
Route::get('/group/2', [Group::class, 'index']);
});
Expand Down

0 comments on commit 457d10d

Please sign in to comment.