تماشای این درس نیاز به اشتراک حرفه‌ای دارد.

Problem: Requiring Blade Directly0:00

Okay, so we have our machine built that's taking a tag blade component like X button, and it's converting it into a raw require statement so that it's really fast, right? So we bypass all the blade stuff. But we're being really naive. We're literally in PHP requiring a blade file. And what happens if you put actual blade inside of that, of course, like this echo, hey, is PHP doesn't know what to do with it.

Plan: Compile Then Require0:27

echo, hey, is PHP doesn't know what to do with it. So it just ends up coming out of the output. And that's no bueno. What we want to do is do an intermediate step where instead of requiring this source file directly, we want to read the source file, actually compile that blade, store it in a new compiled file inside of storage views like Laravel does, and then reference that

Creating Compiled View Path0:44

new compiled file inside of storage views like Laravel does, and then reference that compiled file instead. Okay. So that's what we're going to do. It's going to be a few steps, but it's nothing we can't handle. All right. So let's start with this. Backwards by creating the compiled path that we want. So storage path.

Backwards by creating the compiled path that we want. So storage path. And then in here we say framework views slash. And then we'll say like if we said food.php right now, and we hit save, let's see what would happen. We hit save. And then over here we say compiled path like this, okay. So it's referencing a storage framework views food.php. That's perfect.

So it's referencing a storage framework views food.php. That's perfect. Food doesn't exist at all. If we load it in the browser, we get an exception because it doesn't exist at all. I'm showing you food just so that you can see it show up right there, but in reality in Laravel, you would create a hash or what Laravel does is creates a hash of the source file name and uses that as the file name.

Hashing Compiled Filenames1:36

the source file name and uses that as the file name. So we'll do that. And we'll just do something. I don't actually know what Laravel does itself. And I didn't take the two seconds to actually look it up. So we're just going to MD five path and make it eight characters long like that . And then instead of foo here, we're going to reference hash, save it. And now we have a hash right in there, okay.

And then instead of foo here, we're going to reference hash, save it. And now we have a hash right in there, okay. So the next step, why is it mad at me about that? Oh, because that's illegal. He, I'm surprised it's still worked. Isn't that interesting? Hmm. Okay. Questioning everything I know about PHP. All right.

Reading and Compiling Contents2:07

Questioning everything I know about PHP. All right. So we have the hash. We need to create the hash. So we create that compiled hash file, but first we need to get compiled PHP contents, right? Okay. Uh, contents equals file get contents, really simple, get the source path, right?

Uh, contents equals file get contents, really simple, get the source path, right? Let me do this so that you can see this. And there we go. We have button. Hey, right? These are the contents. Perfect. Uh, now we want to get the compiled contents. How do we do this?

Uh, now we want to get the compiled contents. How do we do this? This is like really cool. If you haven't seen this, blade has compiled string. It's a method you can call and pass any just raw contents. And it will compile it as a string and give you back the PHP version of the string. So let's DD that compiled and check it out. There we go. So now we can just take that content, write it to that compiled file.

Writing and Using Compiled File2:56

There we go. So now we can just take that content, write it to that compiled file. So file put contents. And we're going to write, you put the, what do you do? You put the, here help me out, file name first. Okay. Compiled path. And now compiled contents like this. So we put those contents. We're referencing them down here.

So we put those contents. We're referencing them down here. Now if I save it, check it out. We're referencing three, four, one, five and we look over here and there it exists. We click on it and it's perfect. It has the compiled output. If I load it in the browser, it works. And I can prove that it works. We go back here and we make a few of these.

And I can prove that it works. We go back here and we make a few of these. It works pretty cool. Love to see it. You'll notice that clearly this is something that we've done. We created this compiled file, you saw step by step using very simple PHP. We didn't append a comment at the end that says what the original source path is. Like Laravel does. So clearly that's a difference, but that should show you that there's nothing

Like Laravel does. So clearly that's a difference, but that should show you that there's nothing magical happening here. Like everything is step by step. We're intercepting blades compiler and we're saying, let's look for a component . Great. Okay. Let's take the name.

Okay. Let's take the name. Let's grab the source contents. Let's compile it to blade. Inside storage frame reviews and let's reference that inside of the compiled output directly and skip all of blade entirely, right? Now ours supports nothing. We don't have we can't there's no props. There's no slots.

We don't have we can't there's no props. There's no slots. We don't do any of that, but we're we don't need to do that because that's just , you know, an implementation detail. What we have here, I think is pretty cool. And we'll just keep building it up as we go. We got a little bit more to do. So let's do it. (dramatic music)

So let's do it. (dramatic music)

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