我写了Illuminate/Broadcasting/Broadcasters/Broadcaster.php
的扩展名,以便可以使用自定义函数(retrieveUser()
)。
我如何让Laravel代替原始版本使用它?
到目前为止,我已经尝试将类保存在app / Providers目录和新的app/Extensions
目录中。
我尝试从config\app.php
调用新类,但我要么得到:
类不存在
或:
调用未定义的方法isDeferred()
我还尝试过创建服务提供商来调用它。
我还尝试创建一个自定义应用程序,将默认应用程序扩展为described here。但是,默认应用程序中似乎没有对Illuminate/Broadcasting
的引用,甚至可以覆盖。
这是新班级:
<?php
namespace App\Extensions;
use Illuminate\Support\Arr;
use Illuminate\Broadcasting\Broadcasters\Broadcaster as Broadcaster;
use App\User;
use Illuminate\Support\Facades\Crypt;
class EncryptedBroadcaster extends Broadcaster
{
protected function retrieveUser($request, $channel)
{
$options = $this->retrieveChannelOptions($channel);
$guards = $options['guards'] ?? null;
if (is_null($guards)) {
$authorization = $request->header('Authorization');
$id = Crypt::decrypt($token);
$user = User::find($id);
return $user;
}
foreach (Arr::wrap($guards) as $guard) {
if ($user = $request->user($guard)) {
return $user;
}
}
}
public function auth($request)
{
$channelName = $this->normalizeChannelName($request->channel_name);
if ($this->isGuardedChannel($request->channel_name) &&
! $this->retrieveUser($request, $channelName)) {
throw new AccessDeniedHttpException;
}
return parent::verifyUserCanAccessChannel(
$request, $channelName
);
}
public function validAuthenticationResponse($request, $result)
{
if (is_bool($result)) {
return json_encode($result);
}
$channelName = $this->normalizeChannelName($request->channel_name);
return json_encode(['channel_data' => [
'user_id' => $this->retrieveUser($request, $channelName)->getAuthIdentifier(),
'user_info' => $result,
]]);
}
public function broadcast(array $channels, $event, array $payload = [])
{
$connection = $this->redis->connection($this->connection);
$payload = json_encode([
'event' => $event,
'data' => $payload,
'socket' => Arr::pull($payload, 'socket'),
]);
foreach ($this->formatChannels($channels) as $channel) {
$connection->publish($channel, $payload);
}
}
}
这是我尝试在config/app.php
中调用它的地方:
'providers' => [
// other existing providers ....
App\Providers\EncryptedBroadcaster::class,
],