Search code examples
phplaravelcommand-linelaravel-artisan

Is it possible or recommended to group Laravel commands into nested groups?


We see Laravel's artisan commands in the typical format of <module>:<action>, so there's...

php artisan migrate:refresh
php artisan cache:clear
// etc.

Third party packages add their vendor name in there, so we get...

php artisan l5-swagger:generate

While that may work for specific packages, what if I want to namespace my application's native commands under the project name as well as the module name? Something along the lines of this:

php artisan myapp:auth:create-admin

As opposed to simply

php artisan myapp:create-admin
// OR
php artisan auth:create-admin

Or maybe I want to go crazy and include my vendor name in there too:

php artisan myname:myapp:auth:create-admin

That's a crazy example, but the simple, one extra level of namespacing will certainly help organizing commands and keeping things looking clean.

Are there any such commands already out there? Is it possible to do this in a manner similar to grouping routes?

I want to avoid creating a "super command" that has sub-commands, something like:

php artisan myapp:command Auth CreateAdmin

Solution

  • following is example of command available on my app :

     ps4
      ps4:editor:create                       Create first data ThemeEditor in the database. Options : update | dump
      ps4:editor:export-css                   Export CSS
      ps4:editor:import-css                   Import CSS
      ps4:editor:import-value                 Create first data ThemeEditor in the database
      ps4:export:document                     Routine to generate static file base on release section of backoffice
      ps4:export:html                         Export Accessbility as html files in export.zip file : Requirement : You should put zip file on propal folder.
      ps4:generate                            Full document generation
      ps4:generate:blur:image                 Generate image blur for one document.
      ps4:generate:blur:zoom                  Regenerate all Zoom base64 blur
      ps4:generate:image                      Generate all images of your document
      ps4:generate:onepage                    generate concat of all html in one page
      ps4:generate:uglify                     Uglify generated html/assets to be spyproof
      ps4:generate:vocalization               Generate mp3 by page for a document
      ps4:import:documents                    Massive document Import
      ps4:import:html                         Import Accessbility content from html files
      ps4:import:vocalisation                 Import vocalisation files
      ps4:import:xml                          Import Accessbility content from xml file
      ps4:lunch-worker                        Lunch worker and check if they are alive
      ps4:media:image                         Post traitment media image
      ps4:media:video                         Post traitment media video
      ps4:migrate:accessibility-content       Migrate Accessibility to raw content
      ps4:monitoring:worker                   Command line to monitor our worker
      ps4:optimize:article                    Optimize image on article view.
      ps4:page:generate                       Full document generation
      ps4:page:reset-name                     Reset name of page per number page
      ps4:pdf:html                            Generate html files for specific document and specific pdf.
      ps4:pdf:image                           Convert pdf in images with different size, format, quality.
      ps4:pd:2txt                             Extract from pdf all text splitted by page.
      ps4:worker                              Spawn worker for image traitment
    

    And you command can be configure by $signature attribute :

    namespace App\Console\Commands\Editor;
    
    use Illuminate\Console\Command;
    
    class CreateCommand extends Command
    {
        /**
         * The name and signature of the console command.
         *
         * @var string
         */
        protected $signature = 'ps4:editor:create {foo}';
    }