document channel classes

This commit is contained in:
Taylor Otwell 2018-01-26 10:04:05 -06:00
parent 9cad46911b
commit 0b6ff03d4e
2 changed files with 56 additions and 0 deletions

View File

@ -13,6 +13,7 @@
- [Authorizing Channels](#authorizing-channels)
- [Defining Authorization Routes](#defining-authorization-routes)
- [Defining Authorization Callbacks](#defining-authorization-callbacks)
- [Defining Channel Classes](#defining-channel-classes)
- [Broadcasting Events](#broadcasting-events)
- [Only To Others](#only-to-others)
- [Receiving Broadcasts](#receiving-broadcasts)
@ -352,6 +353,55 @@ Just like HTTP routes, channel routes may also take advantage of implicit and ex
return $user->id === $order->user_id;
});
<a name="defining-channel-classes"></a>
### Defining Channel Classes
If your application is consuming many different channels, your `routes/channels.php` file could become bulky. So, instead of using Closures to authorize channels, you may use channel classes. To generate a channel class, use the `make:channel` Artisan command. This command will place a new channel class in the `App/Broadcasting` directory.
php artisan make:channel OrderChannel
Next, register your channel in your `routes/channels.php` file:
use App\Broadcasting\OrderChannel;
Broadcast::channel('order.{order}', OrderChannel::class);
Finally, you may place the authorization logic for your channel in the channel class' `join` method. This `join` method will house the same logic you would have typically placed in your channel authorization Closure. Of course, you may also take advantage of channel model binding:
<?php
namespace App\Broadcasting;
use App\User;
use App\Order;
class OrderChannel
{
/**
* Create a new channel instance.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Authenticate the user's access to the channel.
*
* @param \App\User $user
* @param \App\Order $order
* @return array|bool
*/
public function join(User $user, Order $order)
{
return $user->id === $order->user_id;
}
}
> {tip} Like many other classes in Laravel, channel classes will automatically be resolved by the [service container](/docs/{{version}}/container). So, you may type-hint any dependencies required by your channel in its constructor.
<a name="broadcasting-events"></a>
## Broadcasting Events

View File

@ -13,6 +13,7 @@
- [The `tests` Directory](#the-tests-directory)
- [The `vendor` Directory](#the-vendor-directory)
- [The App Directory](#the-app-directory)
- [The `Broadcasting` Directory](#the-broadcasting-directory)
- [The `Console` Directory](#the-console-directory)
- [The `Events` Directory](#the-events-directory)
- [The `Exceptions` Directory](#the-exceptions-directory)
@ -110,6 +111,11 @@ A variety of other directories will be generated inside the `app` directory as y
> {tip} Many of the classes in the `app` directory can be generated by Artisan via commands. To review the available commands, run the `php artisan list make` command in your terminal.
<a name="the-broadcasting-directory"></a>
#### The Broadcasting Directory
The `Broadcasting` directory contains all of the broadcast channel classes for your application. These classes are generated using the `make:channel` command. This directory does not exist by default, but will be created for you when you create your first channel. To learn more about channels, check out the documentation on [event broadcasting](/docs/{{version}}/broadcasting).
<a name="the-console-directory"></a>
#### The Console Directory