12

When I install Pusher package, I got an error "Class 'Pusher' not found".

jameshfisher
  • 34,029
  • 31
  • 121
  • 167

6 Answers6

37

Claudio's diagnosis is correct, the namespace Pusher was added in version 3; but changing the Laravel files is not a recommended solution.

A better way is to create an alias in config/app.php. Under the 'aliases' key, add this to the array in the "Third Party Aliases" section:

'Pusher' => Pusher\Pusher::class,
Larry Reinhard
  • 371
  • 2
  • 3
  • This is the best answer. I recently upgraded my Laravel installation from 5.4 to 5.5. I added ```php "pusher/pusher-php-server": "~3.0" ```php to my composer.json file and then added the alias and it worked perfectly. – Humble Hermit Aug 04 '19 at 23:28
4

(OP posted the following answer in the question. The underlying issue is that version 3 of pusher-php-server introduces a namespace and so now requires use Pusher\Pusher.)

Create this command:

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);
    }
}

Then add "php artisan fix:pusher" to composer.json file:

"post-update-cmd": [
   "php artisan fix:pusher",
   "Illuminate\\Foundation\\ComposerScripts::postUpdate",
   "php artisan optimize"
]
jameshfisher
  • 34,029
  • 31
  • 121
  • 167
4

With the version 3 of Pusher I realized that there is changed the namespace for Pusher\Pusher. If configure by composer when set it out the .env, BROADCAST_DRIVER=pusher, it's showing that error. Checking out the log, you can find out where is the proble, located at this file:

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

. It's necessary change the references for Pusher\Pusher instead of Pusher like the image: enter image description here

then find out the function PusherBroadCaster and change the reference Pusher for Pusher\Pusher.

enter image description here

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

4

Use this command and install pusher. It worked for me in Laravel and PHP 8.0.8 perfectly without any additions of code changes.

composer require pusher/pusher-php-server
Shadab K
  • 1,677
  • 1
  • 16
  • 25
0

It's worth checking the version of Pusher used in composer.json. At this moment if you got stuck with implementing Pusher in Laravel 8 or 9 then your pusher-php-server version should be close to 7. At this moment the version of pusher is 7.1.0 (beta)* and therefore you should change composer.json entry to

"pusher/pusher-php-server": "^7.0",
Dibyendu Mitra Roy
  • 1,604
  • 22
  • 20
-3

Just go to vendor/laravel/framework/src/Illuminate/Broadcasting/Broadcasters/PusherBroadcaster.php and change "Use Pusher" to "Use Pusher/Pusher";

Jake Lee
  • 7,549
  • 8
  • 45
  • 86
  • Editing source file is not a good idea. If you cannot override the functionality, it's best to look for some other way of doing things – PHPer Nov 10 '20 at 02:13