Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony - how to know if console command was run from controller or it was run from terminal?

I am building Symfony Application that use console commands. The same console command can be executed from controller thrue events, but it also can be run from terminal. How can I figure it out from where the command was run so that I can implement the user authentication if the command was run from terminal. If the command was run from controller then user already has permission to run. But if it has been run from terminal he must authenticate by username and password so that I check if he has necessary role?

like image 465
sanof Avatar asked Dec 04 '22 00:12

sanof


1 Answers

You can check if your command was run from the console or from a controller using php_sapi_name() function or PHP_SAPI constant (which is similar to php_sapi_name())

if ('cli' === PHP_SAPI) {
    // command was run from the console
} else {
    // command was run from a controller
}
like image 109
Mikhail Prosalov Avatar answered Jan 03 '23 20:01

Mikhail Prosalov