home | title | heroImage | heroImageDark | heroText | actions | features | ||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
true |
Home |
/images/logo.png |
/images/logo_dark.png |
|
|
Create a function:
function sayHelloTo(string $name): string
{
return "Hello, $name!";
}
Setup Invoke:
use Invoke\Invoke;
Invoke::create([
"sayHelloTo",
])->serve();
Start a server, invoke the function:
curl "localhost/invoke/sayHelloTo?name=Kitty"
# response will be: { "result": "Hello, Kitty!" }
Create Post and Comment types:
use Invoke\Data;
use Invoke\Toolkit\Validators\ArrayOf;
class PostResult extends Data
{
public int $id;
public string $title;
#[ArrayOf(CommentResult::class)]
public array $comments;
}
use Invoke\Data;
use Invoke\Toolkit\Validators\Length;
class CommentResult extends Data
{
public int $id;
public string $message;
}
class CommentInput extends Data
{
#[Length(min: 3, max: 255)]
public string $message;
}
Create methods for getting posts and creating comments:
use Invoke\Method;
class GetPosts extends Method
{
protected PostsRepository $postsRepository;
public function __construct(PostsRepository $postsRepository)
{
$this->postsRepository = $postsRepository;
}
protected function handle(): array
{
$posts = $this->postsRepository->getWithComments();
return PostType::many($posts);
}
}
class CreateComment extends Method
{
protected CommentsRepository $commentsRepository;
public function __construct(CommentsRepository $commentsRepository)
{
$this->commentsRepository = $commentsRepository;
}
protected function handle(int $postId, CommentInput $comment): CommentResult
{
$newComment = $this->commentsRepository->create($postId, $comment);
return CommentResult::from($newComment);
}
}
Setup Invoke:
use Invoke\Invoke;
Invoke::create([
"getPosts" => GetPosts::class,
"createComment" => CreateComment::class,
])->serve();
Start a server, invoke the functions:
curl "localhost/invoke/getPosts"
curl -X POST "localhost/invoke/createComment" \
--data '{ "postId": 123, comment: { "message: "Great post!" } }'