Search code examples
laravellaravel-artisanlumen

Laravel Lumen call artisan command from route


In Laravel, I can do this to call an Artisan command from a route:

Route::get('/foo', function () {
    $exitCode = Artisan::call('email:send', [
        'user' => 1, '--queue' => 'default'
    ]);

    //
});

But I can't find an obvious way to do this in the Lumen framework. The error thrown is:

Fatal error: Class 'App\Http\Controllers\Artisan' not found

Solution

  • This was actually very simple. Just be sure to use the Artisan Facade class wherever needed:

    use Illuminate\Support\Facades\Artisan;
    ...
    public function process()
    {
        Artisan::call('command');
    }
    

    I assumed the normal Laravel facades weren't available in the framework by default but they are.

    Also, in bootstrap/app.php, $app->withFacades(); must be uncommented as @tptcat reminded me in the comments.