2017 © Pedro Peláez
 

library commander

A Laravel Backpack-driven interface for executing artisan commands

image

jsiebach/commander

A Laravel Backpack-driven interface for executing artisan commands

  • Sunday, July 1, 2018
  • by jsiebach
  • Repository
  • 4 Watchers
  • 14 Stars
  • 108 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 4 Forks
  • 1 Open issues
  • 1 Versions
  • 20 % Grown

The README.md

Backpack for Laravel Commander

Backpack Commander is a simple CRUD interface for running Artisan commands in Backpack for Laravel. The main benefit is to quickly allow non-developers to run Artisan commands without needing command line access, and to easily configure the arguments and options for the command line using Backpack field definitions., (*1)

Commands CRUD Running a command using Backpack for Laravel Commander, (*2)

Installation

composer require jsiebach/commander, (*5)

  • Publish the config file

php artisan vendor:publish --provider="JSiebach\Commander\CommanderServiceProvider" --tag="config", (*6)

  • Publish the migration

php artisan vendor:publish --provider="JSiebach\Commander\CommanderServiceProvider" --tag="migrations", (*7)

  • Review the config file to update configuration for your app. Available options are:

route: Edit the route for the commander interface. Defaults to /admin/commander/command., (*8)

allow_creation_and_deletion: Determine whether new commands can be added or deleted. It is recommended to keep this option false in a production environment and add new commands directly in the database., (*9)

  • Run php artisan migrate, (*10)

  • Add your commands interface link to the sidebar (Optional), (*11)

<li><a href="{{ backpack_url('commander')."/command" }}"><i class="fa fa-bullhorn"></i> <span>Commands</span></a></li>, (*12)

Adding commands to Commander

You can now add new commands to your commander_commands table. The fields are:, (*13)

command: The artisan command (ie. inspire - whatever you would fill in for php artisan XXXXX), (*14)

descriptive_name: A name to show users for the command. Will default to the artisan command name, (*15)

The CRUD interface will also show a column with the description property of the command., (*16)

Defining commands

To define a command, add a function to the command called getCommanderFields(), which should return an array of Backpack field definitions., (*17)

  • For simple commands like inspire, you can leave off the getCommanderFields() function entirely., (*18)

  • For commands that take arguments and options, you should return the Backpack field configuration in the getCommanderFields() function on the command. For example:, (*19)

<?php

namespace App\Console\Commands;

use App\User;
use Illuminate\Console\Command;
use JSiebach\Commander\CommandableInterface;

class RunNewUsersReport extends Command implements CommandableInterface
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'reports:run-new-users-report {--startdate=} {--include-admins} {--delivery-email=}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Run a report listing the newest users';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        // You can use the arguments of the artisan command as normal:
        $this->comment('The email address to send the report to is '.$this->option('email'));
        $this->comment('The option \'include-admins\' is '.(((boolean) $this->option('include-admins')) ? "true" : "false"));
        $this->comment('The start date for the report is '.$this->option('start-date'));
    }

    /**
     * @return array
     */
    public function getCommanderFields() {
        return [
            [
                'name' => '--startdate',
                'type' => 'date_picker',
                'label' => 'Start Date'
            ],
            [
                'name' => '--include-admins',
                'type' => 'checkbox',
                'label' => 'Include Admin Users'
            ],
            [
                'name' => '--delivery-email',
                'type' => 'select2_from_array',
                'options' => User::all()->pluck('name', 'email'),
                'label' => 'Report Recipient'
            ]
        ];
    }
}

Notes

  • You cannot use relationship fields, since there is no model to be related to. Stick to simple field types that return strings, booleans, or arrays.

Running a command

In the commands CRUD interface, click the Run button to run a command. You will see a form generated by the fields defined on the command., (*20)

Fill out the fields and click Run. The output of the command will be printed in the resulting view., (*21)

Queueing a command

If you want to run a command on the queue, add the field --queue_command. If you want this to be an option, you can use a check box. If you want it to be automatic, use a hidden field with value 1., (*22)

You can specify the name of the queue with the --queue_name option. Defaults to default, (*23)

Future development plans

  • Handle command errors
  • Consider ways to generate the index view without having a table (ie. define $commandable = true on commands that should be included)

Support

Please feel free to submit issues or pull requests on Github., (*24)

License

MIT, (*25)

Credits

Backpack for Laravel, (*26)

The Versions

01/07 2018

dev-master

9999999-dev https://github.com/jsiebach/commander

A Laravel Backpack-driven interface for executing artisan commands

  Sources   Download

MIT

The Requires

 

laravel artisan laravel backpack