Skip to content

Instantly share code, notes, and snippets.

@icedfish
Created July 26, 2015 08:59
Show Gist options
  • Save icedfish/d337740fbf728ed6dd6a to your computer and use it in GitHub Desktop.
Save icedfish/d337740fbf728ed6dd6a to your computer and use it in GitHub Desktop.
Laravel artisan command to run php script.
<?php namespace App\Console\Commands;
use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputArgument;
class RunFile extends Command
{
/**
* The console command name.
*
* @var string
*/
protected $name = 'run';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Run php file with system env support.';
/**
* Execute the console command.
*
* @return mixed
*/
public function fire()
{
$file = $this->argument('php_file');
if (is_file($file)) {
include $file;
} else {
$this->error("file '{$file}' not found!");
}
}
/**
* Get the console command arguments.
*
* @return array
*/
protected function getArguments()
{
return array(
array('php_file', InputArgument::REQUIRED, 'The php file to run with Laputa.'),
);
}
}
@icedfish
Copy link
Author

simple run script like shell php artisan run xxx.php

@fhefh2015
Copy link

Thanks for your share!

Laravel 5.7 is running incorrectly, I modified it, working now!

How to use?

//php artisan run [file path]
php artisan run ./app/Utils/Tools.php
<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputArgument;

class RunFile extends Command
{
    /**
     * The console command name.
     *
     * @var string
     */
    protected $name = 'run';
    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Run php file with system env support.';
    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $file = $this->argument('php_file');
        if (is_file($file)) {
            include $file;
        } else {
            $this->error("file '{$file}' not found!");
        }
    }
    /**
     * Get the console command arguments.
     *
     * @return array
     */
    protected function getArguments()
    {
        return array(
            array('php_file', InputArgument::REQUIRED, 'The php file to run with Laputa.'),
        );
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment