phplaravellaravel-5.1laravel-artisan

How to get the current console command in Laravel


I can see if a script is running in the console with the App::runningInConsole() command, but I would like to know (for audit logging purposes) which command has been run from console.

To add some context - I need to log whenever a system or user accesses a certain type of data. For users, that's pretty simple - I can use Auth::user() and get their IP address from Request, but for console commands, it's a little more difficult.

I can figure out if a console command is running, which is good, but I need to be able to record which console command is running.


Solution

  • I do not see a way to get that information directly Laravel. I'm also not sure how that would be possible, as you can have one command executing other commands or even creating a new application instance like some test tools do.

    There is however a way to achieve that using plain PHP. You can check the server/environment variables to identify how application was executed.

    Have a look at

    dd($_SERVER['argv']);
    

    If you run a command this should give you the command name in $_SERVER['argv'][1]

    You can find more information here: http://php.net/manual/en/reserved.variables.argv.php