具有身份验证用户的Laravel绑定类
如何将类与具有身份验证用户的Laravel绑定类,laravel,authentication,Laravel,Authentication,如何将类与auth user绑定?这是我到目前为止试过的,但不起作用 $this->app->bind(NotificationInterface::class, function($app){ return new Notification(Auth::user()) }); 这是我的通知类 class Notification implements NotificationInterface { use NotificationCollectTrait;
auth user
绑定?这是我到目前为止试过的,但不起作用
$this->app->bind(NotificationInterface::class, function($app){
return new Notification(Auth::user())
});
这是我的通知类
class Notification implements NotificationInterface
{
use NotificationCollectTrait;
/**
* @var Guard
*/
private $auth;
/**
* Notification constructor.
* @param Guard $auth
* @throws Exception
*/
public function __construct(Guard $auth)
{
$this->auth = $auth;
if(!$this->auth->check())
throw UnauthorizedException::notLoggedIn();
# Setup Notifications
$this->notifications();
}
....
这是我的控制器,我想在这里注入接口
class Notification extends Controller{
protected $notification;
public function __construct(NotificationRepositoryInterface $notification)
{
$this->notification = $notification;
}
...
我已经找到了一个解决方案,这种方法很有效,谢谢@lagbox帮助我
public function __construct()
{
$this->middleware(function($request, $next){
$this->repository = app(NotificationInterface::class);
return $next($request);
});
}
这是控制器构造欢迎来到SO。。。什么“不起作用”?$this->app->bind(NotificationInterface::class,function($app){$guard=$app->make(guard::class);返回新通知($guard);});我想我已经找到了一个绑定的解决方案,但我不知道为什么在尝试注入控制器时不工作?构造(NotificationInterface$notification)身份验证是错误的,因为还没有会话,当控制器的构造函数运行请求时,请求还没有通过中间件堆栈,这意味着没有会话,所以没有身份验证。我该如何解决?您能在类或构造函数中讨论如何处理该接口的这个实现吗