我想知道Laravel 5.1中不同的类似命令的类之间有什么区别。据我所知,Laravel 5.1具有以下功能:

  • Console commands (artisan make:console)
  • Commands (artisan make:command)
    • Handlers (artisan make::command --handler)
  • Jobs (artisan make:job)



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?


  • 它们之间的真正(语义和功能)区别是什么?
  • “运行”它们的正确方法是什么?
  • 对于我需要以某种我认为合适的方式运行的一般独立代码段而言,哪种方法最合适?


相关但不完全相同(也没有答案):Laravel 5.1命令和作业





Example: A console command with fires a command to fetch images from Imgur. The Class FetchImages contains the actual business logic of fetching images.


Class which contains the actual logic. You should also be able to call this command from your application with app()->make(Command::class)->handle().



I made this app with Laravel 5.0 so jobs weren't a thing back then. But as I see it, Jobs are like commands but they are queued and can be dispatched. (As you may have seen in those examples, those commands implement your mentioned Interfaces SelfHandling and ShouldBeQueued).

I see myself as an experienced Laravel Developer but those changes in Commands and Jobs are quite difficult to understand.

编辑: 从Laravel文档中:

app / Commands目录已重命名为app / Jobs。但是,您不需要将所有命令都移到新位置,并且可以继续使用Artisan命令make:command和handler:command来生成类。      同样,app / Handlers目录已重命名为app / Listeners,现在仅包含事件侦听器。但是,不需要移动或重命名现有的命令和事件处理程序,并且可以继续使用handler:event命令来生成事件处理程序。      通过为Laravel 5.0文件夹结构提供向后兼容性,您可以将应用程序升级到Laravel 5.1,并在方便您或您的团队时将事件和命令缓慢升级到它们的新位置。