This commit is contained in:
2023-07-03 22:56:59 +08:00
parent f01ea83447
commit bced72d811
4 changed files with 202 additions and 159 deletions
+1 -1
View File
@@ -76,7 +76,7 @@ class ConstrictResponse extends Message implements ResponseInterface
* @param int $statusCode * @param int $statusCode
* @return $this * @return $this
*/ */
public function html(string $content, int $statusCode = 200): static public function html(string $content = '', int $statusCode = 200): static
{ {
$this->getBody()->write($content); $this->getBody()->write($content);
return $this->withContentType(ContentType::HTML)->withStatus($statusCode); return $this->withContentType(ContentType::HTML)->withStatus($statusCode);
+23
View File
@@ -0,0 +1,23 @@
<?php
namespace Kiri\Router;
use Kiri\Router\Base\Controller;
use Psr\Http\Message\ResponseInterface;
class OptionsController extends Controller
{
/**
* @return ResponseInterface
*/
public function index(): ResponseInterface
{
return $this->response->withHeaders(['Access-Control-Allow-Headers' => '*',
'Access-Control-Request-Method' => '*',
'Access-Control-Allow-Origin' => '*'
])->html('');
}
}
+1 -1
View File
@@ -89,7 +89,7 @@ class Response implements ResponseInterface
* @param int $statusCode * @param int $statusCode
* @return ResponseInterface * @return ResponseInterface
*/ */
public function html(string $content, int $statusCode = 200): ResponseInterface public function html(string $content = '', int $statusCode = 200): ResponseInterface
{ {
return $this->__call__(__FUNCTION__, $content, $statusCode); return $this->__call__(__FUNCTION__, $content, $statusCode);
} }
+28 -8
View File
@@ -58,23 +58,33 @@ class Router
/** /**
* @param string $route * @param string $route
* @param string $handler * @param string $handler
* @throws * @param bool $enableOption
* @throws ReflectionException
*/ */
public static function post(string $route, string $handler): void public static function post(string $route, string $handler, bool $enableOption = true): void
{ {
$router = Kiri::getDi()->get(DataGrip::class)->get(static::$type); $router = Kiri::getDi()->get(DataGrip::class)->get(static::$type);
$router->addRoute([RequestMethod::REQUEST_POST], $route, $handler); $router->addRoute([RequestMethod::REQUEST_POST], $route, $handler);
if ($enableOption) {
$options = [di(OptionsController::class), 'index'];
$router->addRoute([RequestMethod::REQUEST_OPTIONS], $route, $options);
}
} }
/** /**
* @param string $route * @param string $route
* @param string $handler * @param string $handler
* @throws * @param bool $enableOption
* @throws ReflectionException
*/ */
public static function get(string $route, string $handler): void public static function get(string $route, string $handler, bool $enableOption = true): void
{ {
$router = Kiri::getDi()->get(DataGrip::class)->get(static::$type); $router = Kiri::getDi()->get(DataGrip::class)->get(static::$type);
$router->addRoute([RequestMethod::REQUEST_GET], $route, $handler); $router->addRoute([RequestMethod::REQUEST_GET], $route, $handler);
if ($enableOption) {
$options = [di(OptionsController::class), 'index'];
$router->addRoute([RequestMethod::REQUEST_OPTIONS], $route, $options);
}
} }
@@ -104,12 +114,17 @@ class Router
/** /**
* @param string $route * @param string $route
* @param string $handler * @param string $handler
* @throws * @param bool $enableOption
* @throws ReflectionException
*/ */
public static function delete(string $route, string $handler): void public static function delete(string $route, string $handler, bool $enableOption = true): void
{ {
$router = Kiri::getDi()->get(DataGrip::class)->get(static::$type); $router = Kiri::getDi()->get(DataGrip::class)->get(static::$type);
$router->addRoute([RequestMethod::REQUEST_DELETE], $route, $handler); $router->addRoute([RequestMethod::REQUEST_DELETE], $route, $handler);
if ($enableOption) {
$options = [di(OptionsController::class), 'index'];
$router->addRoute([RequestMethod::REQUEST_OPTIONS], $route, $options);
}
} }
@@ -128,12 +143,17 @@ class Router
/** /**
* @param string $route * @param string $route
* @param string $handler * @param string $handler
* @throws * @param bool $enableOption
* @throws ReflectionException
*/ */
public static function put(string $route, string $handler): void public static function put(string $route, string $handler, bool $enableOption = true): void
{ {
$router = Kiri::getDi()->get(DataGrip::class)->get(static::$type); $router = Kiri::getDi()->get(DataGrip::class)->get(static::$type);
$router->addRoute([RequestMethod::REQUEST_PUT], $route, $handler); $router->addRoute([RequestMethod::REQUEST_PUT], $route, $handler);
if ($enableOption) {
$options = [di(OptionsController::class), 'index'];
$router->addRoute([RequestMethod::REQUEST_OPTIONS], $route, $options);
}
} }