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

مرور قراردادهای نام‌گذاری منبع‌دار0:00

All right, welcome back, everybody. So you know what? Let's take a short pause on our notes exercise, and instead just take a few minutes to focus on simple cleanup and organization, and I'll show you what I mean. Right now, in the controllers directory, I see three files dedicated to notes, or our notes resource. And then if I open the views directory, the exact same thing is true. Okay, so now you can imagine for a real project, where you have lots of resources, like notes, and posts, and users, and categories, and tags, and fill in the blank, very quickly with this flat directory structure, I don't know, it just gets a little messy.

Organizing Notes Files0:33

and posts, and users, and categories, and tags, and fill in the blank, very quickly with this flat directory structure, I don't know, it just gets a little messy. So with that in mind, why don't we put everything related to notes within its own directory? Okay, notes, and then I'll grab these three and drag them in. Perfect. And then I'll do the exact same thing in the views directory, notes, grab these three, and move them over. Okay, so yeah, it just cleans it up that much. So next, of course, I need to return to my routes file and update the paths. So now I would have things like this, notes, but you'll notice, now that these notes controllers

Updating Route Paths1:07

So next, of course, I need to return to my routes file and update the paths. So now I would have things like this, notes, but you'll notice, now that these NotesControllers are stored within their own directory, maybe we could use a different naming convention. Maybe just something a little more traditional and adhering to common conventions. So for example, right here, notice that I originally named this file noteCreate. And I did that because if it was simply create, well, you wouldn't have known. Is it the create controller for a note, or a user, or a project, or a category? You don't know. So that's why we made it clear. But now that it's in a note directory, or I'm sorry, a notes directory, I don't have

So that's why we made it clear. But now that it's in a note directory, or I'm sorry, a notes directory, I don't have to do that anymore. So I can clean this up, like so. Okay, so now I would just return to my controller, and I would update this like so. Cool. All right, let's keep going. Is there anything else we could do here? Well, again, following common conventions, the controller or action for displaying all of a resource, or a paginated list of a resource, is often called index.

Renaming Controllers Conventionally2:07

Well, again, following common conventions, the controller or action for displaying all of a resource, or a paginated list of a resource, is often called index. So if we took that approach, we would rename this like so. Okay, so just come along for the ride. I'll rename this to index. All right, what else? Okay, right here, this next controller is responsible for showing a single note, right? So again, following conventions, we're going to change that to show. And then I will update this. Cool.

And then I will update this. Cool. So now we have index for showing all, or a list of notes. We have show for showing a single note. We have create for displaying a form to create a new note. I think this looks a lot better. And yeah, more importantly even, we're going to follow this very approach, the same naming convention for every resource in our application. And you'll find when you do that, it makes the whole project that much easier to reason about, to get around, to work on a larger team, because again, everyone is following

And you'll find when you do that, it makes the whole project that much easier to reason about, to get around, to work on a larger team, because again, everyone is following the same convention. So for example, maybe in the future you have a resource for managing Users. And maybe you need to, six months from now, go to the Controller that is responsible for displaying a form to create a new User. All right, well, it's not like you have to go to your routes file and visit your lookup table. You just follow the convention. You know it would be something like users/create, and that is the benefit here.

Renaming Views Conventionally3:34

You just follow the convention. You know it would be something like users/create, and that is the benefit here. For every resource, we use the exact same naming convention. Okay, so if we do that for controllers, we're going to do the exact same thing for our views. So within notes, I would change this to show.view. This one would be index.view. And then this one would be create.view. Okay, finally, I just have to update those paths. So I'll do that now. And I think we'll be in good shape here.

Fixing Broken Partial Paths4:06

So I'll do that now. And I think we'll be in good shape here. Okay, here's one to show. And then this one here at the bottom. view, notes, create. All right, let's do a sanity check. Is everything still working? And of course it fails, but at least we know why. So yeah, because we moved the view to a new subdirectory, the path to the corresponding partial here is no longer correct.

So yeah, because we moved the view to a new subdirectory, the path to the corresponding partial here is no longer correct. So we need to fix that. And I'll show you how. So if I go into the view for this controller, yeah, this is where we require that file and it's not working. There's two ways we could fix this. One option would be to use a relative path from the current file. So we could do something like this, __DIR__, and that will give me the directory of the current file.

So we could do something like this, __dir, and that will give me the directory of the current file. So in this case, views/notes. And then from that point, we could concatenate and go up a level into the views folder, then into the partials folder, and then to the nav.php file. And that should work. So notice if I give it a refresh, we'll still see a warning, but it will be for a different file. Yeah, now it's trying to require nav.php. And that would be fine.

Yeah, now it's trying to require nav.php. And that would be fine. Another option is to use a full path from the system root. So I could say we'll go into the views directory and then into the partials directory. And I think that would work as well. Yeah, and we get the same thing. All right, why don't we stick with that since it's a little quicker. So I will update this, and then down here at the bottom as well. And that should solve the problem, and it does. OK, so now I just need to update this in these files.

ResourcesCommon Action Names

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