Laravel Lumen从路线呼叫工匠指挥

Jar*_*ier 6 laravel artisan lumen

在Laravel中,我可以这样做Artisan从路线调用命令:

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

    //
});
Run Code Online (Sandbox Code Playgroud)

但我无法在Lumen框架中找到一种明显的方法.抛出的错误是:

Fatal error: Class 'App\Http\Controllers\Artisan' not found
Run Code Online (Sandbox Code Playgroud)

Jar*_*ier 12

这其实很简单.只要确保useArtisan Facade任何需要的地方上课:

use Illuminate\Support\Facades\Artisan;
...
public function process()
{
    Artisan::call('command');
}
Run Code Online (Sandbox Code Playgroud)

我认为默认情况下框架中没有正常的Laravel外墙,但它们是.

此外,在bootstrap/app.php,$app->withFacades();作为@tptcat提醒我的意见一定是注释.


Yor*_*gen 5

这只是一个扩展,也许不是最好的方法。但是如果您只是不想使用 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');
Run Code Online (Sandbox Code Playgroud)

或者为以下创建一个别名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');
Run Code Online (Sandbox Code Playgroud)