【发布时间】:2019-10-29 07:38:30
【问题描述】:
我正在使用Laravel Framework 5.8.21。我正在尝试将interface 绑定到基于AppServiceProvider 的register 方法中的Request 参数的类,如下所示。
public function register()
{
$this->app->bind('App\Contracts\SomeInterface', 'App\Logic\ClassName');
if(Request::has('notification_type') && Request::get('notification_type') == 'email') {
$this->app->bind('App\Contracts\SomeInterface', 'App\Logic\SomeotherClassName');
}
}
然后将接口注入Controllers __construct() 方法。
通过测试,它总是绑定到ClassName。我试图在AppServiceProvider 中获取访问的 URL,并且在运行unit tests 时,它总是返回/ 和$this->app->request->getRequestUri();,方法为GET,即使从测试中我发布到这样的URL。
$this->post('/notification', [
'notification_type' => 'email',
])->assertJson(
'message-push-status' => true,
]);
在使用 Postman 对其进行测试时,当我尝试发布 http://localhost:8000/notification 时,它会显示 419 | Page Expired.
【问题讨论】:
标签: php laravel laravel-5 dependency-injection service-provider