Creating Twitter Service0:00
Okay, check out this next one. It's sure to piss off a ton of people. Yes, we have facades in Laravel, but in Laravel 5.4, you have real-time, on-demand facades. So let me show you what I mean by that. It's going to blow your mind. So let's create a services folder, and maybe we'll have like a little Twitter service. Namespace will be App\Services\Twitter. And then, let's see, we're going to have a method called publish, right? Whenever you accept a status. Don't worry, we're not building this up totally. Just some pseudocode. So then here, maybe we will defer to curl. We will post to some endpoint. We're just going to give it the status here.
Adding Curl Dependency0:33
So then here, maybe we will defer to curl. We will post to some endpoint. We're just going to give it the status here. And we'll accept that, like so. Make that an instance of curl or something. Okay, so we need to create that. App\Services\Curl. App\Services\Curl. And then finally, we need a method called post. And again, you'd need the endpoint and all of that stuff. But for the sake of a demo, we're going to say using curl to post the status.
Traditional Resolution vs Facade0:56
And again, you'd need the endpoint and all of that stuff. But for the sake of a demo, we're going to say using curl to post the status. Okay, so does this all make sense? We have a Twitter class that has a publish method. And when you call it, it's going to defer to some dependency to post the status. Now traditionally, you'd need to new up this class, provide the dependency. Actually, in this case, Laravel should be able to resolve it automatically. But nonetheless, you'd have to pull it out of the container or set up a factory for it. However, instead, we could use app, services, Twitter. But with real-time facades, if you just prefix it with the facades namespace,
Using Real-Time Facades1:28
However, instead, we could use app, services, Twitter. But with real-time facades, if you just prefix it with the facades namespace, Laravel will generate a facade in real-time, which means you can interact with it exactly as if it was a facade. For example, Twitter, publish. Here is my status. All right, let's try this out in the browser. Give it a refresh. And what? It works.
