Artisan Call output in Controller?

28,466

Solution 1

$command = 'foo:bar';

$params = [
        'datum' => $request->get('date'),
];

Artisan::call($command, $params);
dd(Artisan::output());

Solution 2

code to output inspire phrases instead of exit code

Route::get('/wisdom', function (Request $request) {
   Artisan::call('inspire');
   return Artisan::output();
});

Solution 3

Some off commands can not run with php artisan in controller you need to run them with shell

public function getCommand($command)
    {
        echo '<br> php artisan ' . $command . ' is running...';
        $output = new BufferedOutput;
        if(strpos($command, 'api') === false && strpos($command, 'passport') === false){
            Artisan::call($command, [], $output);
        }else{
            shell_exec('php ../artisan ' . $command);
            dump('php ../artisan ' . $command);
        }
        dump($output->fetch());
        echo 'php artisan ' . $command . ' completed.';
        echo '<br><br><a href="/admin/setting/advance">Go back</a>';
    }

This is the list of commands and api:gen and passport install just will run with shell from /bootstrap folder !

$commands = [
            [
                'id' => 1,
                'description' => 'recompile classes',
                'command' => 'clear-compiled',
            ],
            [
                'id' => 2,
                'description' => 'recompile packages',
                'command' => 'package:discover',
            ],
            [
                'id' => 3,
                'description' => 'run backup',
                'command' => 'backup:run',
            ],
            [
                'id' => 4,
                'description' => 'create password for passport',
                'command' => 'passport:client --password',
            ],
            [
                'id' => 5,
                'description' => 'install passport',
                'command' => 'passport:install',
            ],
            [
                'id' => 6,
                'description' => 'create a document for api',
                'command' => 'apidoc:generate',
            ],
            [
                'id' => 7,
                'description' => 'show list of routes',
                'command' => 'route:list',
            ],
            [
                'id' => 8,
                'description' => 'recompile config cache',
                'command' => 'config:cache',
            ],
            [
                'id' => 9,
                'description' => 'clear config cache',
                'command' => 'config:clear',
            ],
            [
                'id' => 10,
                'description' => 'run lastest migrations',
                'command' => 'migrate',
            ],
            [
                'id' => 11,
                'description' => 'run seeders',
                'command' => 'db:seed',
            ],
            [
                'id' => 12,
                'description' => 'recompile route cache',
                'command' => 'route:cache',
            ],
            [
                'id' => 13,
                'description' => 'clear route cache',
                'command' => 'route:clear',
            ],
            [
                'id' => 14,
                'description' => 'recompile view cache',
                'command' => 'view:cache',
            ],
            [
                'id' => 15,
                'description' => 'clear view cache',
                'command' => 'view:clear',
            ],
            [
                'id' => 16,
                'description' => 'optimize all configurations',
                'command' => 'optimize',
            ],
        ];
Share:
28,466

Related videos on Youtube

user1469734
Author by

user1469734

Updated on December 17, 2020

Comments

  • user1469734
    user1469734 over 3 years

    I have a complex Artisan Command that I wanna call in my Controller also. That works. Except that it return an Exitcode instead of output.

    use Symfony\Component\Console\Output\BufferedOutput; # on top
    
    public function foobar(Request $request)
    {
        $this->validate($request, [
            'date' => 'required|date_format:Y-m-d',
        ]);
    
        $output = new BufferedOutput;
    
        $exitCode = Artisan::call('foo:bar', [
            'datum' => $request->get('date'),
        ], $output);
        return $exitCode; # returns 0;
        return dd($output->fetch()); # returns ""
    }
    

    I want the output of the command. How to do that? The last line of my Artisan command has a return on the last line that should be returned.. How?

    • Safoor Safdar
      Safoor Safdar almost 8 years
      Artisan:output may help you.
  • user1469734
    user1469734 almost 8 years
    Why only with dd()? And then it returns all the $this->info('foobar') things. But I'm returning at the end an array that I wanna re-use.
  • zorx
    zorx almost 8 years
    dd() is just to show you the response, if you want to use the result somewhere so all you have to do is return Artisan::output(); instead of dd(..)
  • Zohaib Hassan
    Zohaib Hassan about 5 years
    dump(trim(Artisan::output())); could be used if you don't want to terminate request there.
  • Raja Khoury
    Raja Khoury about 4 years
    Thank you Farid.
  • Martin Joiner
    Martin Joiner over 3 years
    Don't forget you may need to import the facade with use Illuminate\Support\Facades\Artisan; before you can reference it.

Related