This commit is contained in:
Malte Schulze Hobeling 2022-12-15 14:55:00 +01:00
parent e3a7e4ea57
commit 26b0cd04bd
3 changed files with 52 additions and 0 deletions

View File

@ -8,6 +8,7 @@ class Response
* @param string $data
* @param int? $status
* @return void
* @author Johannes Kantz
*/
public function send(string $data, int $status = null)
{
@ -22,6 +23,7 @@ class Response
* @param array $data
* @param int? $status
* @return void
* @author Johannes Kantz
*/
public function json(array $data, int $status = null)
{
@ -36,6 +38,7 @@ class Response
* Set the status code
* @param int $status
* @return void
* @author Johannes Kantz
*/
public function status(int $status)
{

View File

@ -5,6 +5,11 @@ class Route
{
private array $methods = [];
/**
* @param string|null $method
* @param $controller
* @author Johannes Kantz
*/
public function __construct(string $method = null, $controller = null)
{
if ($method != null && $controller != null) {
@ -12,36 +17,66 @@ class Route
}
}
/**
* @param $controller
* @return $this
* @author Johannes Kantz
*/
public function get($controller)
{
$this->methods["GET"] = $controller;
return $this;
}
/**
* @param $controller
* @return $this
* @author Johannes Kantz
*/
public function post($controller)
{
$this->methods["POST"] = $controller;
return $this;
}
/**
* @param $controller
* @return $this
* @author Johannes Kantz
*/
public function put($controller)
{
$this->methods["PUT"] = $controller;
return $this;
}
/**
* @param $controller
* @return $this
* @author Johannes Kantz
*/
public function delete($controller)
{
$this->methods["DELETE"] = $controller;
return $this;
}
/**
* @param $controller
* @return $this
* @author Johannes Kantz
*/
public function all($controller)
{
$this->methods["ALL"] = $controller;
return $this;
}
/**
* @param string $method
* @return mixed
* @author Johannes Kantz
*/
public function getController(string $method)
{
return $this->methods[$method] ?? $this->methods["ALL"];

View File

@ -16,6 +16,7 @@ class Router
/**
* Router
* @author Johannes Kantz
*/
public function __construct(string $pathname)
{
@ -44,6 +45,7 @@ class Router
* @param string $uri
* @param $middleware
* @return void
* @author Johannes Kantz
*/
public function use(string $uri, callable $middleware): void
{
@ -54,6 +56,7 @@ class Router
* get Middleware for uri
* @param string $uri
* @return array
* @author Johannes Kantz
*/
private function getMiddleware(): array
{
@ -70,6 +73,7 @@ class Router
* Calls the middleware
* @param array $middleware
* @return void
* @author Johannes Kantz
*/
private function callMiddleware(array $middleware): void
{
@ -83,6 +87,7 @@ class Router
* @param string $uri
* @param $controller
* @return void
* @author Johannes Kantz
*/
public function get(string $uri, callable $controller): void
{
@ -94,6 +99,7 @@ class Router
* @param string $uri
* @param $controller
* @return void
* @author Johannes Kantz
*/
public function post(string $uri, callable $controller): void
{
@ -105,6 +111,7 @@ class Router
* @param string $uri
* @param $controller
* @return void
* @author Johannes Kantz
*/
public function put(string $uri, callable $controller): void
{
@ -116,6 +123,7 @@ class Router
* @param string $uri
* @param $controller
* @return void
* @author Johannes Kantz
*/
public function delete(string $uri, callable $controller): void
{
@ -127,6 +135,7 @@ class Router
* @param string $uri
* @param $controller
* @return void
* @author Johannes Kantz
*/
public function all(string $uri, callable $controller): void
{
@ -137,6 +146,7 @@ class Router
* Route
* @param string $uri
* @return void
* @author Johannes Kantz
*/
public function route(string $uri): Route
{
@ -149,6 +159,7 @@ class Router
* @param string $uri
* @param array $params
* @return string
* @author Johannes Kantz
*/
private function routeMatches(string $route, string $uri): bool
{
@ -175,6 +186,7 @@ class Router
* @param string $uri
* @param array $params
* @return array
* @author Johannes Kantz
*/
private function getParams(string $route, string $uri): array
{
@ -193,6 +205,7 @@ class Router
/**
* gets the controller for the given uri
* @param array $routes
* @author Johannes Kantz
*/
private function getController(): callable|bool
{
@ -211,6 +224,7 @@ class Router
/**
* Starts the router
* @return void
* @author Johannes Kantz
*/
public function start(): void
{