英文:
Make model authenticable when it is extending another class
问题
我在我的应用程序中使用了spatie/laravel-event-sourcing
。我有一个Consultant
模型,它扩展了Projection
(抽象)类。
<?php
namespace App\Projections;
use Spatie\EventSourcing\Projections\Projection;
class Consultant extends Projection
{
我为Consultant
模型创建了身份验证保卫,并且在使用Breeze登录时遇到了这个错误:
Illuminate\Auth\EloquentUserProvider::validateCredentials(): Argument #1 ($user) must be of type Illuminate\Contracts\Auth\Authenticatable, App\Projections\Consultant given
这是因为Consultant
应该扩展Illuminate\Foundation\Auth\User
。
我该如何扩展两个类?Projection
和User
https://github.com/spatie/laravel-event-sourcing/discussions/372
我知道PHP不支持多继承。
问题是核心Laravel代码和包都有类的类型检查,所以特性无法解决这个问题。我可以移动功能,但是某些方法仍然期望Illuminate.Contracts.Auth.Authenticatable
或Spatie.EventSourcing.Projections.Projection
的参数类型。
英文:
I'm using spatie/laravel-event-sourcing
in my app. I have Consultant
model which extending Projection
(abstract) class.
<?php
namespace App\Projections;
use Spatie\EventSourcing\Projections\Projection;
class Consultant extends Projection
{
I made auth guard for Consultant
model and using breeze login I getting this error:
Illuminate\Auth\EloquentUserProvider::validateCredentials(): Argument #1 ($user) must be of type Illuminate\Contracts\Auth\Authenticatable, App\Projections\Consultant given
It is thrown because Consultant should extend Illuminate\Foundation\Auth\User
How I can extend two classes?
Projection
and User
https://github.com/spatie/laravel-event-sourcing/discussions/372
I know that php doesn't support multiple inheritance.
Problem is that core laravel code and package have type check of classes, so traits can't solve this issue. I can move functionality, but some methods still expects argument type of Illuminate\Contracts\Auth\Authenticatable
or Spatie\EventSourcing\Projections\Projection
答案1
得分: 1
感谢您的提示,唯一的解决方案是创建另一个类,并实现所有所需的数据:
namespace App\Contracts;
use Illuminate\Auth\Authenticatable;
use Illuminate\Auth\MustVerifyEmail;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
use Illuminate\Foundation\Auth\Access.Authorizable;
use Spatie\EventSourcing\Projections\Projection;
class AuthenticableProjection extends Projection implements
AuthenticatableContract,
AuthorizableContract,
CanResetPasswordContract
{
use Authenticatable;
use Authorizable;
use CanResetPassword;
use MustVerifyEmail;
}
但不幸的是,在下一步中,我遇到了特定于项目的异常,唯一的解决办法是重写身份验证逻辑。我不会这样做,而是创建另一个模型并建立它们之间的关系。
Spatie\EventSourcing\Projections\Exceptions\ReadonlyProjection
在这一点上,`App\Projections\Consultant` 投影不可写,请首先调用 `$model->writeable()`。
英文:
Thank you for tips,
the only solution was create another class, and implement all needed data:
<?php
namespace App\Contracts;
use Illuminate\Auth\Authenticatable;
use Illuminate\Auth\MustVerifyEmail;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
use Illuminate\Foundation\Auth\Access\Authorizable;
use Spatie\EventSourcing\Projections\Projection;
class AuthenticableProjection extends Projection implements
AuthenticatableContract,
AuthorizableContract,
CanResetPasswordContract
{
use Authenticatable;
use Authorizable;
use CanResetPassword;
use MustVerifyEmail;
}
but unfortunately in next step I'm getting project specific exception and the only way to fix it is to rewrite authentication logic. I won't do that, instead of it I will create another model and make relationship between them
Spatie \ EventSourcing \ Projections \ Exceptions \ ReadonlyProjection
The `App\Projections\Consultant` projection is not writeable at this point, please call `$model->writeable()` first.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论