jackiedo/laravel-pusher

Laravel 5+ 的 Pusher 桥接器

1.0.3 2017-05-17 07:11 UTC

This package is auto-updated.

Last update: 2024-09-11 19:54:21 UTC


README

Latest Stable Version Total Downloads Latest Unstable Version License

描述

Laravel Pusher 是一个从 5.1 版本到 5.4 版本的 Laravel Pusher 桥接器,使用 官方 Pusher 包

此包是从 vinkla/laravel-pusher 包派生而来,以支持使用早期 7.x 版本的 PHP 的 Laravel 5.4。

概述

查看以下主题之一以了解有关 Laravel Pusher 的更多信息。

安装

  • 首先,使用 Composer,在项目的根目录中引入此包。
$ composer require jackiedo/laravel-pusher
  • 一旦更新操作完成,第二步是添加服务提供者。打开 config/app.php,并将新项目添加到 providers 数组中
...
'providers' => array(
    ...
    Jackiedo\LaravelPusher\PusherServiceProvider::class,
),
  • 第三步是在 config/app.php 文件中的 aliases 部分添加以下行
'Pusherer' => Jackiedo\LaravelPusher\Facades\Pusherer::class,

配置

Laravel Pusher 需要连接配置。要开始,您需要发布配置文件

$ php artisan vendor:publish --provider="Jackiedo\LaravelPusher\PusherServiceProvider" --tag="config"

这将创建一个可在其中修改以设置配置的 config/pusher.php 文件。同时,请确保检查此包中原始配置文件在版本之间的更改。

默认连接名称

此选项 default 是您可能指定要作为所有工作的默认连接使用以下哪个连接的地方。当然,您可以使用管理器类一次使用多个连接。此设置的默认值为 main

Pusher 连接

此选项 connections 是您为应用程序设置的每个连接的地方。已包含示例配置,但您可以根据需要添加任意数量的连接。

使用方法

PusherManager

这是最有兴趣的类。它绑定到 ioc 容器中的 pusher,可以使用 Facades\Pusherer 门面访问。此类通过扩展 AbstractManager 实现 ManagerInterface。该接口和抽象类都是 Graham Campbell 的 Graham CampbellLaravel Manager 包的一部分,因此您可能想查看该存储库的文档以了解如何使用管理器类。请注意,返回的连接类始终是 Pusher 的实例。

Pusherer 门面

此门面将动态将静态方法调用传递到 ioc 容器中的 pusher 对象,默认为 PusherManager 类。

PusherServiceProvider

此类不包含任何有趣的公共方法。此类应添加到 config/app.php 中的 providers 数组。此类将设置 ioc 绑定。

示例

在这里,您可以看到一个如何使用此包的示例。开箱即用,默认适配器是 main。在配置文件中输入您的认证详情后,它就会正常工作

Pusherer::trigger('my-channel', 'my-event', ['message' => $message]);
// We're done here - how easy was that, it just works!

Pusherer::getSettings();
// This example is simple and there are far more methods available.

Pusher 管理器将表现得像 Pusher 一样。如果想要调用特定连接,可以使用连接方法

// Writing this…
Pusherer::connection('main')->log('They see me logging…');

// …is identical to writing this
Pusherer::log('They hatin…');

// and is also identical to writing this.
Pusherer::connection()->log('Tryin to catch me testing dirty…');

// This is because the main connection is configured to be the default.
Pusherer::getDefaultConnection(); // This will return main.

// We can change the default connection.
Pusherer::setDefaultConnection('alternative'); // The default is now alternative.

如果您像我一样偏好使用依赖注入而非外观模式,则可以注入管理器

use Jackiedo\LaravelPusher\PusherManager;

class Foo
{
    protected $pusher;

    public function __construct(PusherManager $pusher)
    {
        $this->pusher = $pusher;
    }

    public function bar()
    {
        $this->pusher->trigger('my-channel', 'my-event', ['message' => $message]);
    }
}

App::make('Foo')->bar();

官方文档

本包中还有一些未在此处文档化的类。这是因为在Laravel中,该包是对官方Pusher包的封装。