0

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?

Jite
  • 5,761
  • 2
  • 23
  • 37

3 Answers3

1

I dont Know if I get it. Cant you use PHP system()? If I'm wrong please explain clearly

http://php.net/manual/en/function.system.php

Mehravish Temkar
  • 4,275
  • 3
  • 25
  • 44
r0ulito
  • 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
0

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.

Nimantha
  • 6,405
  • 6
  • 28
  • 69
Jite
  • 5,761
  • 2
  • 23
  • 37
0

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.

Thomas
  • 8,426
  • 1
  • 25
  • 49