Skip to content

0.8.0

Latest
Compare
Choose a tag to compare
@kawanamiyuu kawanamiyuu released this 21 Jul 22:47
6f1250f

add Request Mapping mechanism (* alpha version)

alpha version means,

  • Response body will be rendered as JSON by default.
  • Handler (= Controller) classes will be parsed to collect mapping information every request. (not cached.)

use K9u\RequestMapper\Annotation\GetMapping;
use K9u\RequestMapper\PathParams;
use Psr\Http\Message\ServerRequestInterface;

class FakeController
{
    /**
     * @GetMapping("/blogs")
     *
     * @return array<string, mixed>
     */
    public function getBlogs(): array
    {
        // snip
        return ...;
    }

    /**
     * @GetMapping("/blogs/{id}")
     *
     * @param ServerRequestInterface $request
     *
     * @return array<string, mixed>
     */
    public function getBlogById(ServerRequestInterface $request): array
    {
        $pathParams = $request->getAttribute(PathParams::class);
        assert($pathParams instanceof PathParams);

        $blogId = (int) $pathParams['id'];

        // snip
        return ...;
    }
}