Laravel commands and jobs

前端 未结 3 1200
死守一世寂寞
死守一世寂寞 2021-01-30 17:35

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:

  • Con
3条回答
  •  逝去的感伤
    2021-01-30 18:01

    I see those "objects" like so: (I added some code examples from one of my side projects)

    Console

    Things I want to execute from the command line (As you mentioned with your example with "Delete Files older than x"). But the thing is, you could extract the business logic of it to a command.

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

    Command

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

    Example: Command mentioned in Example 1. Contains logic which does the actual API calls to Imgur and process returned data.

    Jobs

    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.

    EDIT: From the Laravel Docs:

    The app/Commands directory has been renamed to app/Jobs. However, you are not required to move all of your commands to the new location, and you may continue using the make:command and handler:command Artisan commands to generate your classes.

    Likewise, the app/Handlers directory has been renamed to app/Listeners and now only contains event listeners. However, you are not required to move or rename your existing command and event handlers, and you may continue to use the handler:event command to generate event handlers.

    By providing backwards compatibility for the Laravel 5.0 folder structure, you may upgrade your applications to Laravel 5.1 and slowly upgrade your events and commands to their new locations when it is convenient for you or your team.

提交回复
热议问题