Skip to content

Instantly share code, notes, and snippets.

@martinbean
Last active March 15, 2019 14:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save martinbean/16399826a790b72c685746c6287f833e to your computer and use it in GitHub Desktop.
Save martinbean/16399826a790b72c685746c6287f833e to your computer and use it in GitHub Desktop.
Mapping jobs to handler classes in Lumen
<?php
namespace App\Handlers\Jobs;
use App\Jobs\LogNameJob;
use Illuminate\Support\Facades\Log;
class LogNameHandler
{
public function __construct()
{
// Inject any dependencies the handler needs
}
public function handle(LogNameJob $job)
{
// Handle the job
Log::debug($job->name); // Will write 'Martin Bean' to log
}
}
<?php
namespace App\Jobs;
class LogNameJob extends Job
{
/**
* The name to log.
*
* @var string
*/
public $name;
/**
* Create a new job instance.
*
* @param string $name
* @return void
*/
public function __construct(string $name)
{
$this->name = $name;
}
}
<?php
namespace App\Providers;
use App\Handlers\Jobs\LogNameHandler;
use App\Jobs\LogNameJob;
use Illuminate\Support\Facades\Bus;
use Illuminate\Support\ServiceProvider;
class LogNameServiceProvider extends ServiceProvider
{
public function boot()
{
Bus::map([
LogNameJob::class => LogNameHandler::class,
]);
}
}
<?php
use App\Jobs\LogNameJob;
use Illuminate\Support\Facades\Bus;
$router->get('/', function () use ($router) {
// When the queue processes LogNameJob,
// it should invoke the LogNameHandler class.
Bus::dispatch(new LogNameJob('Martin Bean'));
return response('Dispatched.');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment