در حال بارگذاری ...

Problem: Previewing Mailables0:00

When you are constructing a new mailable, sometimes it can get a little tricky, because you're preparing the markdown view, but you don't exactly know how it's going to turn out. So generally what you end up doing is you use a service like MailTrap, or you physically send the email and then you just take a look at it, and you keep doing that back and forth. And that works just fine. However, in Laravel 5.5, we can also render it directly in the browser, simply by newing up the mail class. Now before we get to that though, let me show you something. Imagine we have a class Test here. And all we're going to do is return a new instance of that Test class. What do you think is going to happen here? Well, let's take a look. Laravel 5.5, and yeah, the response content must be a string, okay? But we gave it an instance of Test.

Understanding Renderable Responses0:34

What do you think is going to happen here? Well, let's take a look. Laravel 5.5, and yeah, the response content must be a string, okay? But we gave it an instance of test. Now let's just take a look at the response class, Illuminate\Http\Response. And if we scroll down, here's where we set the content. Now specifically, yeah, it checks to see, like, should we render JSON? Then let's morph it to JSON. But now take a look at this one right here. If the content implements this class called Renderable, and if we take a look at that, it's just an interface that has a render method, well, then we will call the render method on the object so we can avoid any two-string exceptions, which is exactly what we got there. Okay, so let's try this. Let's have it implement Renderable, and there's the full namespace. Now if we leave it like that, it's going to fail because we're not implementing the interface. So let's make sure we have a

Implementing the render Method1:15

Let's have it implement Renderable, and there's the full namespace. Now if we leave it like that, it's going to fail because we're not implementing the interface. So let's make sure we have a method called render, and I'm just going to stub it out here. Let's say we're going to return some contents of a view, or just an h1 here. If I now switch back and refresh, there we go, it works. So what we can see here is if the object implements the Renderable interface, then the Laravel's response class is going to trigger this render method and then use whatever you return from here. Okay, so what was the reason for going over that? Well, let's do this. Let's build up a new mailable, so php artisan make:mail, and we'll use the typical welcome email, and we'll say the markdown template should be, how about emails.welcome. Okay, so now you'll know that we create a welcome.php file, and when we build it, this is the view it builds up, and if we take a look at that,

Rendering a Mailable in Browser2:01

template should be, how about emails.welcome. Okay, so now you'll know that we create a welcome.php file, and when we build it, this is the view it builds up, and if we take a look at that, yeah, it's just a simple little stub here. So now we're going to come back and new up that class, return new App\Mail\Welcome. Okay, back to Chrome, give it a refresh, and sure enough, we can now see once again in Laravel 5.5, we can now see the contents or the layout of the mail. Now, if you're curious about this, well, we go to the Welcome class, and if we scroll up, that extends the Mailable class. So let's go there, and you'll see, yep, Mailable implements the Renderable contract. So if we take a look at that, yep, the Response class or object will call this render method and build up the view. So that's how it's working behind the scenes. So now, yeah, you have a really easy way to start constructing these things. Welcome to Laracasts, and then add some lorem text,

دوست دارید گاهی خبرهای Laracasts را ایمیل کنیم؟