Channel Authorization Overview0:00
First up, we have BroadcastChannel classes. This is new in 5.6. So if I go to my routes/channels folder, you'll see I have one set up here. This project is actually from a series at Laracasts called Get Real with Laravel Echo. So if this entire thing I'm talking about is foreign to you, I'd highly recommend going through this and I'll break it down step by step. Anyways, in this file, you can register essentially your authorizations that determines does the authenticated user have permission to access this channel, and participate in this channel, and receive notifications from this channel. So you can imagine if you're using something like Pusher, and from your server side maybe
Testing 403 Access1:35
If I open up the console, yep, I don't see any errors. Let's instead open up an incognito tab. And now if we run this, and I open up the console, you'll see that we get a 403 Forbidden. So notice we hit that endpoint. We're trying to figure out, does the person accessing this project have permission to receive updates? And the answer is no. Now in this case, only for the series, we're giving anyone access to this page. But in real life, you would of course lock that down as well. Anyways, let's get to it.
Introducing Channel Classes2:05
But in real life, you would of course lock that down as well. Anyways, let's get to it. In Laravel 5.6, you can now extract all of this to a dedicated class. And you might want to reach for this if you get to the point where you have a lot of different channels and it's starting to get a little messy, and you want to clean things up a bit. OK. Only on that condition should you consider it. Otherwise, I think this is actually fairly clean. All right. So if we run php artisan under the make namespace, you'll see a new one to make a new channel.
Generating a Channel Class2:27
All right. So if we run php artisan under the make namespace, you'll see a new one to make a new channel. Let's give that a run. php artisan make:channel. And this is kind of related to the project, so I'll call it ProjectChannel. And you'll see that this now creates an app/broadcasting directory. So if we open this up, of course you can pass anything through the constructor, and you now have a new join method. And this is a good name. So the contents here should determine whether or not this authenticated user may join that.
And this is a good name. So the contents here should determine whether or not this authenticatedUser may join that channel. So you could hard code it if you want, or in real life you would do a check, something just like this. Reformat. Let me pull in the project. And there we go. So now if you're curious, well how did we get project into this join method? Well that's being defined right here.
Implicit Binding in Channels3:15
So now if you're curious, well how did we get project into this join method? Well that's being defined right here. And you'll notice what I like about this is it's almost identical to how you would define your routes. It's very similar, and that's why it's contained within that same folder. Here's your web.php routes, and then here are your channel routes, so to speak. So in this case, you can see I'm using a wildcard. So when I pass this through, we're going to use implicit model binding to track down the associated project. OK, so let's take a look at this.
Refactoring to Class Reference3:41
associated project. OK, so let's take a look at this. I don't need a constructor in this case. And we now have our BroadcastChannel class. Now if we come back to our channels file, I can get rid of the contents here entirely and just replace it with a reference to the class. I'll import that, like so. And that should do it. So one more time, I'll give this a refresh. On the private tab where we are not authenticated, we will get a 403, which is what we expect.
So one more time, I'll give this a refresh. On the private tab where we are not authenticated, we will get a 403, which is what we expect. However, if we close that out and now I'm in my active tab where I'm signed in, once again we open up the console, and just like before, we still have permission. So everything's working exactly as it did before. So that's what's new. php artisan make:channel.
