-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #42 from tab-engineer/master
添加rest路由
- Loading branch information
Showing
16 changed files
with
146 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,7 +19,7 @@ | |
}, | ||
"require": { | ||
"php": "^8.0", | ||
"max/utils": "^1.0" | ||
"max/utils": "dev-master" | ||
}, | ||
"extra": { | ||
"max": { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,6 @@ | |
"require": { | ||
"php": "^8.0", | ||
"psr/http-message": "^1.0", | ||
"max/utils": "^1.0" | ||
"max/utils": "dev-master" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
<?php | ||
|
||
namespace Max\Routing; | ||
|
||
class RestRouter | ||
{ | ||
/** | ||
* Rest路由规则 | ||
* | ||
* @var array|array[] | ||
*/ | ||
protected static array $maps = [ | ||
'index' => [['GET', 'HEAD'], '/%s'], | ||
'show' => [['GET', 'HEAD'], '/%s/{id}'], | ||
'store' => [['POST'], '/%s'], | ||
'update' => [['PUT', 'PATCH'], '/%s/{id}'], | ||
'delete' => [['DELETE'], '/%s/{id}'], | ||
]; | ||
|
||
/** | ||
* @var Route | ||
*/ | ||
protected Route $index; | ||
/** | ||
* @var Route | ||
*/ | ||
protected Route $show; | ||
/** | ||
* @var Route | ||
*/ | ||
protected Route $store; | ||
/** | ||
* @var Route | ||
*/ | ||
protected Route $update; | ||
/** | ||
* @var Route | ||
*/ | ||
protected Route $delete; | ||
|
||
public function __construct( | ||
protected RouteCollector $routeCollector, | ||
protected string $uri, | ||
protected string $controller, | ||
protected array $middlewares = [], | ||
protected array $patterns = [], | ||
) { | ||
foreach (static::$maps as $action => $map) { | ||
[$methods, $path] = $map; | ||
$this->routeCollector->addRoute($this->{$action} = new Route( | ||
$methods, | ||
sprintf($path, $uri), | ||
[$this->controller, $action], | ||
$this->patterns, | ||
$this->middlewares, | ||
)); | ||
} | ||
} | ||
|
||
/** | ||
* @return Route | ||
*/ | ||
public function getIndex(): Route | ||
{ | ||
return $this->index; | ||
} | ||
|
||
/** | ||
* @return Route | ||
*/ | ||
public function getShow(): Route | ||
{ | ||
return $this->show; | ||
} | ||
|
||
/** | ||
* @return Route | ||
*/ | ||
public function getStore(): Route | ||
{ | ||
return $this->store; | ||
} | ||
|
||
/** | ||
* @return Route | ||
*/ | ||
public function getUpdate(): Route | ||
{ | ||
return $this->update; | ||
} | ||
|
||
/** | ||
* @return Route | ||
*/ | ||
public function getDelete(): Route | ||
{ | ||
return $this->delete; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters