I was wondering what the difference is between the different command-like classes in Laravel 5.1. As far as I can tell Laravel 5.1 has the following available:
- Console commands (
artisan make:console
) - Commands (
artisan make:command
)- Handlers (
artisan make::command --handler
)
- Handlers (
- Jobs (
artisan make:job
)
I have come straight from 4.2 to 5.1 so I don't know what happened in between 4.2 and 5.1, but I have been told that the middle one (just commands) are basically not really supposed to be used any more - they are in from when queue-able jobs became 'commands' in 5.0, but Laravel since decided against this, and they're just in for compatibility. However, I'm not 100% on this point, so clarification would be appreciated.
My specific use-case is that I want a place to put a self-contained 'runnable' task. For example, something that will remove files older than 5 days from a given directory (but it could do anything).
At first this sounds like a console command - I want to be able to run it from artisan
, for a start. But I may also want it on a schedule (great, artisan schedule:run
runs console commands). But I may also want to execute it asynchronously from code. Console commands can be run synchronously with Artisan::call()
, but for asynchronous, this is (I think) where queues come in, and it suddenly has to be a job.
Okay so we have a job. We can now add it to a queue from code, but how do we execute it as an artisan command (synchronously)? Can I just create a thin console command and add the DispatchesJobs
trait (or the code therein) to it, and then dispatch the job? Does the job always have to go on a queue, or can we make a job execute synchronously (and, ideally, output to the console command's output?) The same question goes for running it on a schedule - am I supposed to create this console command and add that to the scheduler, or can I make the scheduler run the job directly?
And finally, we have 'commands' that aren't console commands nor are they jobs. As I said before, people tell me these are just hangers-on from a Laravel 5.0 code change that was (kinda) reverted. But the artisan make
command still exists for them, so they can't be that dead. Also, what's the deal with a self handling command (the default, comes with a handle
method) and one that 'requires' a handler class (run artisan make:command --handler
)? How do you actually make these execute? Manually with (new App\Command\SomeCommand)->handle();
or (new App\handlers\SomeCommandHandler)->handle(new App\Command\SomeCommand)
, or is there some hidden system I don't know about (maybe they can be dispatched using the job/queue dispatcher)? Also you can create 'queued' commands artisan make::command --queued
, so how do these differ, too?
I guess my question boils down to the following:
- What is the real (semantic and functional) difference between them all?
- What is the correct way to 'run' them?
- Which is best for my purposes of a generally-standalone bit of code that needs to be run, in whatever manner I feel appropriate?
I found information in the documentation on how to use queues and create console commands, but nothing on exactly when to use them or really anything on command classes and handlers.
Related but not exactly the same (also, it's unanswered): Laravel 5.1 commands and jobs