1. Go to this page and download the library: Download bowphp/cqrs library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
bowphp / cqrs example snippets
use Bow\CQRS\Command\CommandInterface;
class CreateUserCommand implements CommandInterface
{
public function __construct(
public string $username,
public string $email
) {}
}
use Bow\CQRS\Command\CommandHandlerInterface;
class CreateUserCommandHandler implements CommandHandlerInterface
{
public function __construct(public UserService $userService) {}
public function process(CommandInterface $command): mixed
{
if ($this->userService->exists($command->email)) {
throw new UserServiceException(
"The user already exists"
);
}
return $this->userService->create([
"username" => $command->username,
"email" => $command->email
]);
}
}
use Bow\CQRS\Registration as CQRSRegistration;
public function run()
{
CQRSRegistration::commands([
CreateUserCommand::class => CreateUserCommandHandler::class
]);
}
namespace App\Controllers;
use App\Controllers\Controller;
use App\Commands\CreateUserCommand;
class UserController extends Controller
{
public function __construct(private CommandBus $commandBus) {}
public function __invoke(Request $request)
{
$payload = $request->only(['username', 'email']);
$command = new CreateUserCommand(
$payload['username'],
$payload['email']
);
$result = $this->commandBus->execute($command);
return redirect()
->back()
->withFlash("message", "User created");
}
}