流明依赖注入

Lumen Dependency Injection

作为 this post 的最后评论,它声明如下:

You can easily do it in any service provider (boot would be a nice place since we can use method based DI).

public function boot(\Illuminate\Contracts\Http\Kernel $kernel) {
 $kernel->appendMiddleware('Sheepy85\L5Localization\Middleware\Localization'); // prependMiddleware works too.
}

这是Laravel用于从服务提供商注入中间件的代码。

我正在尝试从 Lumen Framework 实现同样的事情,这里是代码:

<?php namespace Acme\Slz\Providers;

use Illuminate\Contracts\Http\Kernel;
use Illuminate\Support\ServiceProvider;

class SlzServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     * @param Kernel $kernel
     * @return void
     */
    public function boot(Kernel $kernel)
    {
        // push the serializer middleware to the stack
        $kernel->pushMiddleware(Acme\Slz\Middleware\Serializer::class);
    }

    /**
     * Register any application services.
     * This service provider is a great spot to register your various container
     * bindings with the application. As you can see, we are registering our
     * @return void
     */
    public function register()
    {
    }
}

但这会引发以下错误:

lumen.ERROR: exception 'ErrorException' with message 'Argument 1 passed to Acme\Slz\Providers\SlzServiceProvider::boot() must be an instance of Illuminate\Contracts\Http\Kernel, none given

是否需要做更多的事情来启用带有 Lumen 的依赖容器?

这个怎么样:

<?php namespace Acme\Slz\Providers;

use Illuminate\Support\ServiceProvider;

class SlzServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        // push the serializer middleware to the stack
        $this->app->middleware([
            'serializer' => 'Acme\Slz\Middleware\Serializer',
        ]);
    }
}