Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.