1. Go to this page and download the library: Download edupham/laravel-command-bus 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/ */
namespace App\Http\Controllers\User\Commands;
class RegisterUser implements \Edupham\CommandBus\Command
{
private $email;
private $password;
public function __construct(string $email, string $password)
{
$this->email = $email;
$this->password = $password;
}
public function email(): string
{
return $this->email;
}
public function password(): string
{
return $this->password;
}
}
namespace App\Http\Controllers\User\Handlers;
class RegisterUserHandler implements \Edupham\CommandBus\Handler
{
private $userRepository;
public function __construct(UserRepository $userRepository)
{
$this->userRepository = $userRepository;
}
public function handle(\Edupham\CommandBus\Command $command): User
{
$user = new User(
$command->email(),
$command->password()
);
$this->userRepository->store($user);
return $user;
}
}
namespace App\Http\Controllers\User\Controllers;
use Edupham\CommandBus\Command;
use Edupham\CommandBus\CommandBus;
class AbstractController extends \Illuminate\Routing\Controller
{
private $dispatcher;
public function __construct(\Edupham\CommandBus\CommandBus $dispatcher)
{
$this->dispatcher = $dispatcher;
}
public function dispatch(\Edupham\CommandBus\Command $command)
{
return $this->dispatcher->execute($command);
}
}
namespace App\Http\Controllers\User\Controllers;
use App\Http\Controllers\User\Commands\RegisterUser;
use Illuminate\Http\Request;
class UserController extends AbstractController
{
public function store(Request $request)
{
$user = $this->dispatch(new RegisterUser(
$request->input('email'),
$request->input('password')
));
return $user;
}
}