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

Opening User Snippets0:00

Okay, let's talk about snippets. So if I hit Shift-Command-P and I look for snippet, let's open user snippets for PHP in this case, and you can see this is blank. But there are some out of the box. Take a look. Snippet, insert snippet, and we can browse through here. I'm actually not sure where these come from. Maybe the IntelliFence package, or maybe it's just available out of the box. I'm not sure. But anyways, you have some basic stuff here. But I rarely use these. I really like to create my own because it commits to memory. So I would always recommend creating your own snippets versus pulling in a package with a bunch of them because you're not going to remember them and you're not going to use them. So some of the common ones I always use and illustrate in a series like this is basic ones for creating a method. So having to do this over and over will just drive you crazy. So let's create a snippet for this. Now,

Creating Method Snippets0:41

and illustrate in a series like this is basic ones for creating a method. So having to do this over and over will just drive you crazy. So let's create a snippet for this. Now, it's a little clunky. I'm going to be honest. It's not the cleanest thing. So it's all stored as a JavaScript object here or JSON. So yes, we could comment this out. And if we want something like method, well, let's just use a prefix of M-E-T. That will be your shortcut. And yeah, it's kind of clunky. I've got to be honest. Not ideal. And then you're figuring out like, are there breaks here? I'm building this up as an array. It's just it's horrible to work with. But let's undo this. There is a package I use. I think it's called Snippet Creator. Let's see. Yeah, this one right here. So this will allow you to take an existing block of code and immediately create a snippet for it. Let's pull that in. And now let's create that again, public function foo.

Using Snippet Creator1:27

Yeah, this one right here. So this will allow you to take an existing block of code and immediately create a snippet for it. Let's pull that in. And now let's create that again, public function foo. So yeah, imagine you have something like this and you think, all right, I want that to be a snippet. I'm going to select it, shift command P, and we have this new one called create snippet. All right. For php, the snippet name will be public method. The shortcut will be M-E-T. That's what I use. A lot of people do an underscore prefix. So like _M will be for method. But I just stick to M-E-T. And then a description, new public method. All right. And you're done. So if I switch over to php.json, you'll see that it built that up for me. And I didn't have to figure out the correct spacing and alignment on my own. But you know what? I think this is actually wrong. And I think that's because, yeah, we would have to bring that over here. So we'll just fix

from any editor you use, going all the way back to TextMate. So let's do $1, a tab, and then $2. Okay, one more time. M-E-T, tab, my method, tab, and then we're good to go. So generally I'll have snippets for this, and then I usually want one for a protected function as well. So let's just set it up the way we would ideally want it to be. And then de-indent. Okay. Create snippet, php. This is a protected method. This one will be P-M-E-T, and we'll say new protected method. Okay, we're all set. We scroll back, we have a new one here. So let's give that one a shot. We'll have a method here called $1, and then a protected method called $2. But now you can actually take this a step further. Let me show you. We'll go back to Safari, and I'm going to look for snippets. Creating your own snippets. So you will have these variables that you can reference. Let's see. So yeah, here's where you can get the file name, and you can parse things to dynamically.

Dynamic Class Snippets3:57

snippets. Creating your own snippets. So you will have these variables that you can reference. Let's see. So yeah, here's where you can get the file name, and you can parse things to dynamically build up your snippet, which is useful for things like creating a new class based upon the file name. That's what you would want there. Why don't we give that a shot, and then we'll call it a day. So we're going to set up, let's clear, actually let's just create a new file. So I'm going to want a class stub. So it will be something like this, but we want that to be based upon the file name. So let's say to start, paste that in, and then we will create a new snippet. All right. That will be class, class, new class. Okay. So here's what we have at the moment. Let's give it a shot. Let's empty this out, and let's see. Insert snippet, class. Oh, but you know what? Yeah. So this isn't going to work because technically we're not in PeachMe mode yet.

give it a shot. Let's empty this out, and let's see. Insert snippet, class. Oh, but you know what? Yeah. So this isn't going to work because technically we're not in PeachMe mode yet. So I bet if we do that now, yeah, now those snippets will populate. So notice here it automatically built up member based upon the file name. So it sounds like we just need to tweak this a little bit. We'll start with a class syntax. Okay. So now we'll say I want a new class like so. Why don't we tweak this though, and we'll make this the first tab stop. All right. One more time, class, and now I can add a method here, and then another protected method. Now what about when we want to pull in other files? So for example, in Laravel, you will often have to do something like this, where you say use Illuminate\Http\Request, and you'll notice we get good auto completion there. Once again, just in case you missed it, that is because of this package here.

Auto-Import Shorthand Uses5:33

something like this, where you say use Illuminate\Http\Request, and you'll notice we get good auto completion there. Once again, just in case you missed it, that is because of this package here. Now rather than doing that, we can use shorthand. So I could just say use Request, and it will automatically find that for me. Very clean. Or let's say right here, I'm going to use the Request class, and once again, if I hit enter, it will automatically use that at the top, which is great. Anyways, that's just an aside. So now that you know how to create file type specific snippets, in the next episode, we'll move on to something else.

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