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?
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With