PHP code example of smoothphp / querybus

1. Go to this page and download the library: Download smoothphp/querybus 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/ */

    

smoothphp / querybus example snippets




return [
    // ...
    
    'providers' => [
        // ...
        SmoothPhp\QueryBus\Laravel\LaravelQueryBusServiceProvider::class,
    ],
];




class FindUserById {
    public $id;
    public function __construct(string $id) {
        $this->id = $id;
    }
}



class FindUserByIdHandler {
    private $client;
    
    public function __construct(DBClient $client) {
        $this->client = $client;
    }
    
    public function handle(FindUserById $query) {
        return $this->client->table('users')->where('id', $query->id)->get();
    }
}



class ExampleController extends Controller {
    private $bus;
    
    public function __construct(\SmoothPhp\QueryBus\QueryBus $queryBus) {
        $this->bus = $queryBus;
    }
    
    public function showUser(string $userId) {
        return view('users.show')->with('user', $this->bus->query(new FindUserById($userId)));
    }
}