Laravel Lumen challenge the artisan team from the route

In Laravel, I can do this to call a command Artisanfrom a route:

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

    //
});

But I cannot find an obvious way to do this within the framework of Lumen. Error thrown:

Fatal error: Class 'App\Http\Controllers\Artisan' not found
+4
source share
2 answers

It was very simple. Just make sure the useclass Artisan Facadewhere necessary:

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

I assumed that normal Laravel facades are not available in the framework by default, but they are.

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

+8
source

, , . , Facade? , Illuminate\Contracts\Console\Kernel.

// See what Artisan facade provides in `Illuminate\Support\Facades\Artisan`
// and thats: `Illuminate\Contracts\Console\Kernel`
app('Illuminate\Contracts\Console\Kernel')->call('command');

Illuminate\Contracts\Console\Kernel:

// In your service provider or bootstrap/app.php create the alias
$this->app->alias('arti', 'Illuminate\Contracts\Console\Kernel');

// now the 'artisan' alias is available in the container
app('arti')->call('command');
+3

Source: https://habr.com/ru/post/1622672/


All Articles