Is it possible to execute command prompt from Laravel controller? If YES, then how can i execute a command and what is the best way to do this? If NO,is there any other way how i can do this?
-
yes you can use shell-commands – Aug 27 '18 at 06:26
-
1shell_exec('whoami') – Aug 27 '18 at 06:26
-
How about PHP exec or shell-exec: http://php.net/manual/en/function.shell-exec.php – hktang Aug 27 '18 at 06:27
-
Also see this one: https://stackoverflow.com/questions/44374495/run-sh-file-using-exec-laravel-php – hktang Aug 27 '18 at 06:27
-
Possible duplicate of [Call shell commands from laravel controller?](https://stackoverflow.com/questions/26527091/call-shell-commands-from-laravel-controller) – Erubiel Aug 27 '18 at 06:29
3 Answers
I dont Know if I get it. Cant you use PHP system()? If I'm wrong please explain clearly

- 4,275
- 3
- 25
- 44

- 487
- 2
- 13
-
Actually, that was my bad, you did not really post a followup question, removed my comment (and will remove this tomorrow so it's clean here, hehe). – Jite Aug 27 '18 at 18:26
Not 100% sure what you mean with "command prompt" but you can execute commands directly from php with the exec
function. This is often not a recommended approach though and I would suspect that it is possible that if you need to run exec
there is a design issue in the application.
I would recommend that you consider other approaches to the problem and only use exec
in very special conditions.
If you only need to run artisan commands (e.g. migrating the database) you can easily do that from your controller:
Route::get('/foo', function () {
$exitCode = Artisan::call('email:send', [
'user' => 1, '--queue' => 'default'
]);
//
});
Source: Laravel Docs
There are some packages available that give you a UI for calling Artisan commands, just search for "laravel artisan web".
If you need to execute other commands you should give us more information about what you're trying to do.

- 8,426
- 1
- 25
- 49