我想知道为安装的软件包添加或调整某些功能的最佳做法是什么。我认为扩展现有的类在我开始时听起来是个好主意,但似乎是一个小小的添加似乎会导致许多新增内容。这是正确的方法吗,下一步是什么?
E.g。我正在使用Laravel / Passport包。在没有详细说明原因的情况下,具有客户端的用户可以发送电子邮件来邀请其他用户。该用户可以在他或她登录时接受邀请。为此,我创建了以下迁移。
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateInvitationRequestsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('oauth_invitation_requests', function (Blueprint $table) {
$table->increments('id');
$table->integer('client_id')->index();
$table->string('name');
$table->string('email');
$table->boolean('accepted');
$table->boolean('declined');
$table->boolean('revoked');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('oauth_invitation_requests');
}
}
以下模型
<?php
namespace App;
use Laravel\Passport\Client;
use Illuminate\Database\Eloquent\Model;
class InvitationRequest extends Model
{
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'oauth_invitation_requests';
/**
* The guarded attributes on the model.
*
* @var array
*/
protected $guarded = [];
/**
* Get all of the invitation requests for the client.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function client()
{
return $this->belongsTo(Client::class);
}
}
在查看\ Laravel \ Passport \ ClientRepository的代码时,我注意到以下代码
<?php
namespace Laravel\Passport;
class ClientRepository
{
...
/**
* Delete the given client.
*
* @param \Laravel\Passport\Client $client
* @return void
*/
public function delete(Client $client)
{
$client->tokens()->update(['revoked' => true]);
$client->forceFill(['revoked' => true])->save();
}
}
因此,当删除客户端时,所有令牌也会被撤销。相关的邀请请求也应该发生同样的情况。所以我添加了以下两个类
<?php
namespace App;
use Laravel\Passport\Client;
use Illuminate\Database\Eloquent\Model;
class PassportClient extends Client
{
/**
* Get all of the invitation requests from the client.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function invitationRequests()
{
return $this->hasMany(InvitationRequest::class, 'client_id');
}
}
和
<?php
namespace App;
use App\PassportClient;
use Laravel\Passport\ClientRepository;
class PassportClientRepository extends ClientRepository
{
/**
* Delete the given client.
*
* @param App\Client $client
* @return void
*/
public function delete(PassportClient $client)
{
$client->invitationRequests()->update(['revoked' => true]);
parent::delete($client);
}
}
我想我也需要这个
<?php
namespace App\Http\Controllers;
use App\PassportClientRepository;
use Laravel\Passport\Http\Controllers\ClientController;
class PassportClientController extends ClientController
{
/**
* Create a client controller instance.
*
* @param \Laravel\Passport\ClientRepository $clients
* @param \Illuminate\Contracts\Validation\Factory $validation
* @return void
*/
public function __construct(PassportClientRepository $clients,
ValidationFactory $validation)
{
$this->clients = $clients;
$this->validation = $validation;
}
}
到目前为止,我觉得这么好吗?但现在我有点卡住了。根据文档,我不得不添加Passport :: routes();在AuthServiceProvider中,它加载路由。我假设我必须覆盖只有一条路线的行为,那将是下一条路线。
+--------+----------+-----------------------------------------+------------------+----------------------------------------------------------------------------+--------------+
| Domain | Method | URI | Name | Action | Middleware |
+--------+----------+-----------------------------------------+------------------+----------------------------------------------------------------------------+--------------+
| | DELETE | oauth/clients/{client_id} | | \Laravel\Passport\Http\Controllers\ClientController@destroy | web,auth |
+--------+----------+-----------------------------------------+------------------+----------------------------------------------------------------------------+--------------+
我对Laravel如何完全加载路线感到有点困惑,并且不确定我应该继承什么类以使其工作。