user1469734
user1469734

Reputation: 801

Artisan Call output in Controller?

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?

Upvotes: 24

Views: 46299

Answers (6)

Chetan
Chetan

Reputation: 345

In Laravel 11, you can use the following syntax.

If you don't have parameters you can use this.

Artisan::call(
    $command,
    outputBuffer: new \Symfony\Component\Console\Output\ConsoleOutput()
);

If you have parameters you can use this.

Artisan::call(
    $command,
    $parameters,
    new \Symfony\Component\Console\Output\ConsoleOutput()
);

Upvotes: 0

sunaoka
sunaoka

Reputation: 41

for Laravel 11.

Artisan::call(
    $command,
    $parameters,
    new \Symfony\Component\Console\Output\ConsoleOutput()
);

Upvotes: 4

luciandex
luciandex

Reputation: 647

If you want to test some artisan command output, then use shell_exec as in above response:

    $output = shell_exec('php artisan run-some:command-1');
    $this->assert....($desired, $output);

Simple Artisan:call... and Artisan::output() will not work in the case you want to output a string if error occurred.

Upvotes: 0

Farid shahidi
Farid shahidi

Reputation: 352

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',
            ],
        ];

Upvotes: 2

Software Developer
Software Developer

Reputation: 868

code to output inspire phrases instead of exit code

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

Upvotes: 12

zorx
zorx

Reputation: 1951

$command = 'foo:bar';

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

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

Upvotes: 66

Related Questions