PHP code example of laravelplus / commander

1. Go to this page and download the library: Download laravelplus/commander library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

laravelplus / commander example snippets


return [
    'enabled' => env('COMMANDER_ENABLED', true),
    
    // Interface Configuration
    'url' => env('COMMANDER_URL', 'admin/commander'),
    'route_prefix' => env('COMMANDER_ROUTE_PREFIX', 'admin'),
    'route_name_prefix' => env('COMMANDER_ROUTE_NAME_PREFIX', 'commander'),
    'middleware' => env('COMMANDER_MIDDLEWARE', ['auth', 'web']),
    
    // Tracking Configuration
    'track_output' => env('COMMANDER_TRACK_OUTPUT', true),
    'track_arguments' => env('COMMANDER_TRACK_ARGUMENTS', true),
    'track_options' => env('COMMANDER_TRACK_OPTIONS', true),
    'track_execution_time' => env('COMMANDER_TRACK_EXECUTION_TIME', true),
    'track_user' => env('COMMANDER_TRACK_USER', true),
    
    // Storage Configuration
    'max_output_length' => env('COMMANDER_MAX_OUTPUT_LENGTH', 10000),
    'retention_days' => env('COMMANDER_RETENTION_DAYS', 90),
];

// Commands to ignore from tracking (still appear in interface)
'ignored_commands' => [
    'schedule:run',
    'queue:work',
    'queue:listen',
    'migrate:*',
    'db:seed',
    'config:cache',
    'route:cache',
    'view:cache',
],

// Commands to exclude from interface (completely hidden)
'excluded_commands' => [
    'vendor:*',
    'package:*',
    'tinker',
    'serve',
    'test:*',
    'dusk:*',
    'make:*',
    'stub:*',
    'clear-compiled',
    'down',
    'up',
    'env',
    'key:generate',
    'optimize',
    'optimize:clear',
    'config:clear',
    'route:clear',
    'view:clear',
    'cache:clear',
    'cache:forget',
    'cache:table',
    'session:table',
    'queue:table',
    'queue:failed-table',
    'queue:batches-table',
    'notifications:table',
    'event:generate',
    'listener:generate',
    'mail:make',
    'middleware:make',
    'provider:make',
    'request:make',
    'resource:make',
    'rule:make',
    'seeder:make',
    'test:make',
    'channel:make',
    'command:make',
    'component:make',
    'controller:make',
    'event:make',
    'exception:make',
    'factory:make',
    'job:make',
    'listener:make',
    'mail:make',
    'middleware:make',
    'model:make',
    'notification:make',
    'observer:make',
    'policy:make',
    'provider:make',
    'request:make',
    'resource:make',
    'rule:make',
    'seeder:make',
    'test:make',
],

// Commands that appear but cannot be executed (read-only)
'disabled_commands' => [
    'migrate:fresh',
    'migrate:refresh',
    'migrate:reset',
    'db:wipe',
    'config:clear',
    'route:clear',
    'view:clear',
    'cache:clear',
    'optimize:clear',
    'event:clear',
    'queue:restart',
    'queue:flush',
    'queue:forget',
    'queue:retry',
    'queue:retry-batch',
    'queue:work',
    'queue:listen',
    'schedule:run',
    'schedule:list',
    'schedule:test',
    'schedule:work',
    'vendor:publish',
    'vendor:install',
    'vendor:update',
    'package:discover',
    'package:clear-cache',
    'package:optimize',
    'package:cache',
    'package:config',
    'package:route',
    'package:view',
    'package:lang',
    'package:migrate',
    'package:seed',
    'package:publish',
    'package:install',
    'package:update',
    'package:uninstall',
    'package:list',
    'package:show',
    'package:outdated',
    'package:update',
    'package:install',
    'package:remove',
    'package:

// Generate commander URL
$url = commander_url(); // http://127.0.0.1:8000/admin/commander
$url = commander_url('list'); // http://127.0.0.1:8000/admin/commander/list

// Generate commander routes
$route = commander_route('index'); // commander.index
$route = commander_route('run', ['command' => 'test']); // commander.run

// Check if commander is enabled
if (commander_enabled()) {
    // Commander functionality is available
}

use LaravelPlus\Commander\Traits\TracksCommandExecution;

class YourController extends Controller
{
    use TracksCommandExecution;

    public function runCommand(Request $request)
    {
        $commandName = 'check:password-expiry';
        $arguments = ['--batch-size=10'];
        $options = ['--delay=2'];

        // Start tracking
        $this->startTracking($commandName, $arguments, $options);

        try {
            // Execute your command
            $result = $this->executeCommand($commandName, $arguments, $options);
            
            // Complete tracking with success
            $this->completeTracking(true, 0, $result['output']);
            
            return response()->json($result);
        } catch (Exception $e) {
            // Complete tracking with failure
            $this->completeTracking(false, 1, $e->getMessage());
            throw $e;
        }
    }
}

use LaravelPlus\Commander\Models\CommandExecution;

// Get last execution time for a command
$lastExecution = CommandExecution::getLastExecutionTime('check:password-expiry');

// Get execution statistics
$stats = CommandExecution::getCommandStats('check:password-expiry', 30);

// Get recent executions
$recentExecutions = CommandExecution::recent(7)->get();

// Get failed executions
$failedExecutions = CommandExecution::failed()->get();
bash
php artisan vendor:publish --tag=commander-migrations
php artisan vendor:publish --tag=commander-config
bash
php artisan migrate
bash
# Run all tests
php vendor/bin/phpunit

# Run specific test suites
php vendor/bin/phpunit --testsuite=Unit
php vendor/bin/phpunit --testsuite=Feature

# Run with coverage report
php vendor/bin/phpunit --coverage-html coverage/

tests/
├── Feature/
│   └── DashboardTest.php          # API endpoint tests
├── Unit/
│   ├── Http/
│   │   ├── Actions/
│   │   │   ├── BaseActionTest.php
│   │   │   ├── GetCommandsActionTest.php
│   │   │   ├── RunCommandActionTest.php
│   │   │   ├── GetDashboardStatsActionTest.php
│   │   │   ├── RetryCommandActionTest.php
│   │   │   ├── ShowDashboardActionTest.php
│   │   │   ├── GetRecentExecutionsActionTest.php
│   │   │   └── SearchCommandsActionTest.php
│   │   └── Controllers/
│   │       └── CommandsControllerTest.php
│   └── TestCase.php               # Base test case
└── phpunit.xml                    # Test configuration