我安装Pusher软件包时出现了错误:“未找到'Pusher'类”。
克劳迪奥的诊断是正确的,命名空间Pusher是在版本3中添加的;但修改Laravel文件不是推荐的解决方法。
更好的方法是在config/app.php
中创建一个别名。在“别名”键下,在“第三方别名”部分的数组中添加以下内容:
'Pusher' => Pusher\Pusher::class,
在Pusher的第3个版本中,我意识到Pusher\Pusher的命名空间已经更改。如果通过composer进行配置并将其设置为.env中的BROADCAST_DRIVER=pusher,则会显示错误。查看日志,您可以找到问题所在的位置,位于此文件中:
'vendor/laravel/framework/src/Illuminate/Broadcasting/BroadcastManager.php"
需要将引用Pusher更改为Pusher\Pusher,就像图片中一样:
然后找到函数 PusherBroadCaster 并将引用Pusher更改为Pusher\Pusher。
vendor/laravel/framework/src/Illuminate/Broadcasting/Broadcasters/PusherBroadcaster.php
使用此命令安装 Pusher。在 Laravel 和 PHP 8.0.8 中,它可以完美地工作,无需添加任何代码或进行修改。
composer require pusher/pusher-php-server
以下是OP在问题中发布的答案。根本问题在于pusher-php-server的第3个版本引入了命名空间,因此现在需要use Pusher\Pusher
。
创建此命令:
namespace App\Console\Commands;
use Illuminate\Support\Facades\File;
use Illuminate\Console\Command;
class FixPusher extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'fix:pusher';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Fix Pusher namespace issue';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$broadcastManagerPath = base_path('vendor/laravel/framework/src/Illuminate/Broadcasting/BroadcastManager.php');
$pusherBroadcasterPath = base_path('vendor/laravel/framework/src/Illuminate/Broadcasting/Broadcasters/PusherBroadcaster.php');
$contents = str_replace('use Pusher;', 'use Pusher\Pusher;', File::get($broadcastManagerPath));
File::put($broadcastManagerPath, $contents);
$contents = str_replace('use Pusher;', 'use Pusher\Pusher;', File::get($pusherBroadcasterPath));
File::put($pusherBroadcasterPath, $contents);
}
}
"php artisan fix:pusher"
添加到composer.json
文件中:"post-update-cmd": [
"php artisan fix:pusher",
"Illuminate\\Foundation\\ComposerScripts::postUpdate",
"php artisan optimize"
]
值得检查composer.json中使用的Pusher版本。目前,如果您在Laravel 8或9中实现Pusher时遇到困难,则pusher-php-server版本应接近7。目前,pusher的版本是7.1.0(beta)*,因此您应该更改composer.json条目为
"pusher/pusher-php-server": "^7.0",
只需前往 vendor/laravel/framework/src/Illuminate/Broadcasting/Broadcasters/PusherBroadcaster.php
并将 "Use Pusher" 更改为 "Use Pusher/Pusher"。
"pusher/pusher-php-server": "~3.0"
然后添加了别名,它完美地工作了。 - Humble Hermit