【发布时间】:2017-12-16 14:19:18
【问题描述】:
当我安装 Pusher 包时,我收到一个错误“找不到 Class 'Pusher'”。
【问题讨论】:
当我安装 Pusher 包时,我收到一个错误“找不到 Class 'Pusher'”。
【问题讨论】:
克劳迪奥的诊断是正确的,命名空间Pusher是在版本3中加入的;但不建议更改 Laravel 文件。
更好的方法是在config/app.php 中创建一个别名。在“别名”键下,将其添加到“第三方别名”部分的数组中:
'Pusher' => Pusher\Pusher::class,
【讨论】:
php "pusher/pusher-php-server": "~3.0" php 添加到我的composer.json 文件中,然后添加了别名,它运行良好。
(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"
]
【讨论】:
在第 3 版 Pusher 中,我意识到 Pusher\Pusher 的命名空间发生了变化。如果在将其设置为 .env 时由 composer 配置,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
【讨论】:
去vendor/laravel/framework/src/Illuminate/Broadcasting/Broadcasters/PusherBroadcaster.php
并将“使用 Pusher”更改为“使用 Pusher/Pusher”;
【讨论】: