laravel 应用中间件后Service Provider中的访问请求

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37555236/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 13:54:50  来源:igfitidea点击:

Access Request in Service Provider After Applying Middleware

laravellaravel-5ioc-container

提问by schellingerht

Bindings

绑定

I'm using bindings in my service provider between interface and implementation:

我在我的服务提供者中在接口和实现之间使用绑定:

public function register()
{
    $this->app->bind('MyInterface', MyImplementation::class);
}

Middleware

中间件

In my middleware, I add an attribute to the request:

在我的中间件中,我向请求添加了一个属性:

public function handle($request, Closure $next)
{
    $request->attributes->add(['foo' => 'bar]);
    return $next($request);
}

Now, I want to access fooin my service provider

现在,我想访问foo我的服务提供商

public function register()
{
    $this->app->bind('MyInterface', new MyImplementation($this->request->attributes->get('foo')); // Request is not available
}

The register() is called before applying the middleware. I know.

在应用中间件之前调用 register()。我知道。

I'm looking for a technique to 'rebind' if the request->attributes->get('foo') is set

如果设置了 request->attributes->get('foo'),我正在寻找一种“重新绑定”的技术

回答by Filip Koblański

Try like this:

像这样尝试:

public function register()
{
    $this->app->bind('MyInterface', function () {
        $request = app(\Illuminate\Http\Request::class);

        return app(MyImplementation::class, [$request->foo]);
    }
}

Binding elements works like this that they will be triggered only when they are call.

绑定元素是这样工作的,它们只有在被调用时才会被触发。

回答by Adam Kozlowski

In service providerYou can also access Request Objectby:

service provider您还可以Request Object通过以下方式访问:

public function register()
{
    $request = $this->app->request;
}

回答by bkm

I know this is old post. But I am having issue using this deferred service provider to define gates.

我知道这是旧帖子。但是我在使用这个延迟服务提供商来定义门时遇到了问题。

I am using Laravel 5.8

我正在使用 Laravel 5.8

Service provider: I want to defer loaded after having defined "foo" (in a middleware).

服务提供者:我想在定义“foo”(在中间件中)后推迟加载。

class MyServiceProvider extends ServiceProvider implements DeferrableProvider
{

 protected $defer = true;

 public function register()
 {
      $this->app->bind('MyImplementation', function () {
          $request = app(\Illuminate\Http\Request::class);
          return app(MyImplementation::class, [$request->foo]);
      }
 }    

 public function provides()
 {
    return [MyImplementation::class];
 }
}

Here is how I handle in a middleware(adding "foo" and use the service provider). "foo" will be used to define Gates.

这是我在中间件中的处理方式(添加“foo”并使用服务提供者)。“foo”将用于定义盖茨。

public function handle($request, Closure $next)
{
    $request->attributes->add(['foo' => 'bar']);
    // Here I load gates
    $myImpl = app(MyImplementation::class);
    $myImpl ->loadGates(); 
    return $next($request);
}

My service: define gate, For testing I just return "true" but never get into Gate::define().

我的服务:定义门,为了测试,我只返回“true”但从未进入 Gate::define()。

class MyImplementation
{
     public function loadGates()
    {
        //dd("Test 1"); // reach here
        Gate::define('create-user', function ($user) {
            dd("Test 2"); //never reach here
            // "foo" will be used here to define Gates.
            return true; 
        });
    }
}

I am wondering why I am not getting into Gate::define().

我想知道为什么我没有进入 Gate::define()。

Am I missing semething here please?

我在这里错过了什么吗?

Thanks

谢谢

回答by The Extendable

Try this

尝试这个

public function register()
{
    $this->app->bind('MyInterface', function ($app) {
        return new MyImplementation(request()->foo);
    }
}