类 'Pusher' 未找到。

12

我安装Pusher软件包时出现了错误:“未找到'Pusher'类”。

6个回答

37

克劳迪奥的诊断是正确的,命名空间Pusher是在版本3中添加的;但修改Laravel文件不是推荐的解决方法。

更好的方法是在config/app.php中创建一个别名。在“别名”键下,在“第三方别名”部分的数组中添加以下内容:

'Pusher' => Pusher\Pusher::class,

这是最好的答案。我最近将我的Laravel安装从5.4升级到了5.5。我在我的composer.json文件中添加了"pusher/pusher-php-server": "~3.0" 然后添加了别名,它完美地工作了。 - Humble Hermit

4

在Pusher的第3个版本中,我意识到Pusher\Pusher的命名空间已经更改。如果通过composer进行配置并将其设置为.env中的BROADCAST_DRIVER=pusher,则会显示错误。查看日志,您可以找到问题所在的位置,位于此文件中:

'vendor/laravel/framework/src/Illuminate/Broadcasting/BroadcastManager.php"

需要将引用Pusher更改为Pusher\Pusher,就像图片中一样:

enter image description here

然后找到函数 PusherBroadCaster 并将引用Pusher更改为Pusher\Pusher。

enter image description here

vendor/laravel/framework/src/Illuminate/Broadcasting/Broadcasters/PusherBroadcaster.php


4

使用此命令安装 Pusher。在 Laravel 和 PHP 8.0.8 中,它可以完美地工作,无需添加任何代码或进行修改。

composer require pusher/pusher-php-server

4

以下是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"
]

0

值得检查composer.json中使用的Pusher版本。目前,如果您在Laravel 8或9中实现Pusher时遇到困难,则pusher-php-server版本应接近7。目前,pusher的版本是7.1.0(beta)*,因此您应该更改composer.json条目为

"pusher/pusher-php-server": "^7.0",

请看这个:https://packagist.org/packages/pusher/pusher-php-server

-3

只需前往 vendor/laravel/framework/src/Illuminate/Broadcasting/Broadcasters/PusherBroadcaster.php 并将 "Use Pusher" 更改为 "Use Pusher/Pusher"。


编辑源文件不是一个好主意。如果您无法覆盖功能,则最好寻找其他方法来完成任务。 - PHPer

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接