by Jason Judge / @JasonDJudge
Hosted Slides
| Github Source
Press "S" for speaker notes
<?php namespace App\Console\Commands;
class Hello extends Command
{
protected $signature = 'demo:hello';
public function handle()
{
$this->info(sprintf(
'Number of arguments: %d',
count($this->argument())
));
}
}
Note:
- A very simple command to tell you how many parameters have been passed in.
- Shows output (info message) and access to arguments.
- As elsewhere, much left out of this for clarity; read the official docs.
<?php namespace App\Console;
class Kernel extends ConsoleKernel
{
// This array registers custom commands with Laravel.
protected $commands = [
...
\App\Console\Commands\Hello::class,
];
}
Note:
- Each command given as the full namespaced name.
$ ./artisan demo:hello 1 2 3
Number of params: 3
$
There are commands to help you create commands.
Can also run a command from a controller.
But why?
Note:
- This is run from a shell prompt here.
- Keep commands for CLI interaction; there are better ways to
run code through a controller.
<?php namespace App\Jobs;
use Illuminate\Contracts\Queue\ShouldQueue;
class CheckOrders extends Job implemkents ShouldQueue
{
public __construct($data_to_work_on) {
$this->data_to_work_on = $data_to_work_on;
}
public function handle(AnyProvider $provider, ...)
{
// Do its stuff
}
}
Note:
- The majority of jobs will be run like this; it is the main
purpose of jobs.
// Register a closure to run when the Order model is updated.
\App\Order::updated(function(\App\Order $order) {
// Do stuff given order
});
Note:
- We register a closure as an "updated" event on the Eloquent model.
// What the eloquent "updated" event registration actually does.
Event::listen(
'eloquent.updated: App\Order', // Event name
function(\App\Order $order) {
// Do stuff given order
}
);
Note:
- This is the name of the event that the previous example constructs and registers for you.
- This name is used here just to understand; it may change in Laravel core, so buyer beware.
<?php namespace App\Providers;
class EventServiceProvider extends ServiceProvider
{
protected $listen = [
'eloquent.updated: App\Order' => [
// Custom listener class to do stuff
'App\Listener\OrderUpdated',
],
...
];
}
Note:
- This $listen array is just a convenince, and nothing special.
<?php namespace App\Providers;
class EventServiceProvider extends ServiceProvider
{
protected $listen = [
'App\Events\OrderComplete' => [
// Custom listener class to do stuff
'App\Listener\OrderComplete',
],
...
];
}
Then fire the `App\Events\OrderComplete` event from the eloquent `updated` event when the
status of the order goes to "COMPLETE".
Note:
- The name of a custom event is its namespaced name.
\App\Order::updated(function(\App\Order $order) {
// Order has changed status to COMPLETE.
if (
$order->getOriginal()['status'] != 'COMPLETE'
&& $order->status == 'COMPLETE'
) {
Event::fire(new App\Events\OrderComplete($order));
}
});
An artisan command will create the event and listener classes from
the array in the event provider.
Note:
- Register this during the application boot; you want to register events and
listeners early.
# crontab
# send outstanding emails every five minutes
*/5 * * * * php artisan demo:send_emails
# send payment reminders at 8am daily
0 8 * * * php artisan demo:send_payment_reminders
# etc
Or let laravel manage the schedules in code for you.
# Call scheduler every minute
* * * * * php artisan schedule:run >/dev/null 2>&1
Note:
- The cron shedule parameters are great, but not friendly.
<?php namespace App\Console;
class Kernel extends ConsoleKernel
{
protected function schedule(Schedule $schedule)
{
$schedule->command('demo:send_payment_reminders')
->dailyAt('8:00');
// etc for other jobs
}
}
The fluent scheduling options build into a cron schedule.
Each minute Laravel then decides which of the console commands to run.
Note:
- There is a great range of fluent schedule options.