【问题标题】:Class 'Pusher' not found未找到“Pusher”类
【发布时间】:2017-12-16 14:19:18
【问题描述】:

当我安装 Pusher 包时,我收到一个错误“找不到 Class 'Pusher'”。

【问题讨论】:

    标签: laravel-5 pusher


    【解决方案1】:

    克劳迪奥的诊断是正确的,命名空间Pusher是在版本3中加入的;但不建议更改 Laravel 文件。

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

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

    【讨论】:

    • 这是最好的答案。我最近将我的 Laravel 安装从 5.4 升级到了 5.5。我将php "pusher/pusher-php-server": "~3.0" php 添加到我的composer.json 文件中,然后添加了别名,它运行良好。
    【解决方案2】:

    (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】:

      在第 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

      【讨论】:

        【解决方案4】:

        vendor/laravel/framework/src/Illuminate/Broadcasting/Broadcasters/PusherBroadcaster.php 并将“使用 Pusher”更改为“使用 Pusher/Pusher”;

        【讨论】:

        • 编辑源文件不是一个好主意。如果你不能覆盖这个功能,最好寻找一些其他的做事方式
        猜你喜欢
        • 2020-03-21
        • 1970-01-01
        • 2020-01-29
        • 2019-10-22
        • 2014-10-24
        • 1970-01-01
        • 2020-07-23
        • 1970-01-01
        • 2018-05-03
        相关资源
        最近更新 更多