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

Fixing Moved File Paths1:49

Okay, so let's have a look. But first, real quick, I noticed at the end of the last video, when I moved a bunch of files to the core directory, these two were excluded. So let's do that right now. I will grab those and move them over. Okay, next, I'll need to update my paths. Our functions file is now stored within the core directory, and the same is true for the router. Next, though, I think things will still break. So if I come back and give this a refresh, yeah, now, because the router is within a

Next, though, I think things will still break. So if I come back and give this a refresh, yeah, now, because the router is within a different directory, it can no longer track down the routes file. So let's fix that very quickly. We'll go in here, and yeah, right down here where we require the routes, let's swap that out with require base_path('routes.php'). Okay, come back, give it a refresh, and now we just have one more fix to make. So once again, within the router file, it can no longer properly locate our controllers. So if we come back, right here is where we require the controller. But once again, let's start at the base path.

Adding Core Namespaces3:14

And in this case, we're sort of grouping our core infrastructure code, or really this is sort of like our miniature framework code. And yeah, we called it core. So now I want to create namespaces for each of these classes that lines up with that directory tree. So if I open up, for example, database, right here at the top, I will give it a namespace of core. And immediately you'll notice when I do that, I get all of these squigglies here next to other classes. And further, if I come back to Firefox and give it a refresh, everything's going to break.

other classes. And further, if I come back to Firefox and give it a refresh, everything's going to break. Because now, well, it doesn't really make sense, right? We're trying to load a class called Database, but we moved it. Now Database is within an entirely different namespace, so of course it can't find it. Okay, so let's have a look at the error here. Class Database not found in notes/index. So let's go into our controller, notes/, excuse me, notes/index. And yeah, once again, we are trying to instantiate our Database class, but again, it's no longer located there.

And yeah, once again, we are trying to instantiate our database class, but again, it's no longer located there. We moved it into a different namespace. Okay, so we can fix this by using the full namespaced path. And in this case, it would be core. And notice rather than a forward slash for the divider, we actually use a backslash, as you see there. And now this quickly goes away. Okay, so this would be one way to do it. Another option is to add the use keyword at the top of your file.

Okay, so this would be one way to do it. Another option is to add the use keyword at the top of your file. So I could say use Core\Database. And now within the rest of the file, I can just reference it like I did before. So yeah, in a way, this is sort of like an alias. This is the full namespaced path that I want to use within this file. And then after that, anywhere else I reference it can simply use the class name alone. That's the way that works. Okay, so if I come back, and I give this a refresh, yeah, we've solved that particular error.

Updating Autoloader for Namespaces5:07

Okay, so if I come back, and I give this a refresh, yeah, we've solved that particular error. But now we've moved on to another one. So you'll see right here, well, we're trying to autoload the database path. But remember in the last episode where I said, well, we're gonna have to rewrite this in the next video, this right here, and yeah, we're now to that point. Now that we're using a namespaced class, well, the logic is going to be entirely different. So let's have a look here. Let's dd the class. And if I come back and give it a refresh, yeah, and as you can see, we're now dealing

Let's die and dump the class. And if I come back and give it a refresh, yeah, and as you can see, we're now dealing with something entirely different. Okay, so let's do this. For our reference, I will comment and paste what class is equal to right now. And let's figure out how we can translate this namespaced path into a require statement. Okay, well, if you think about it, I don't want to use this backslash. It needs to be a forward slash usually, or whatever the directory separator is for your OS. So what if we did something like this?

OS. So what if we did something like this? php has a function called str_replace. And you can see here, we search for some kind of sequence of characters, and then we specify what we want to replace those characters with. And then we provide the thing that we are looking at. So in this case, the subject, the thing we are looking at is class. We are going to look for a, excuse me, we're going to look for a \. But in this case, we actually have to escape the \ to make it crystal clear that I literally mean a \ character, rather than something that I'm escaping, in this

But in this case, we actually have to escape the \ to make it crystal clear that I literally mean a backslash character, rather than something that I'm escaping, in this case a quote. Okay, so look for a \ and replace it with, on my machine, it would be a /. But again, why don't we make it dynamic by using DIRECTORY_SEPARATOR, so that we use whatever is appropriate for your OS, for your OS. Okay, so let's have $results and take a look at that in the browser. And if I come back and I give it a refresh, yeah, we just swapped out that \ with a /.

And if I come back and I give it a refresh, yeah, we just swapped out that backslash with a forward slash. Okay, that helps. So now when I make my require right down here, require base_path core, well, I no longer have to hard code that. That gives us this. We could swap out results, or I don't see any reason to create a new variable. So let's just overwrite class at that point, and I can substitute it here. Okay, let's come back, give it a refresh, and yeah, it may not look like it here, but I think we actually solved the problem.

Okay, let's come back, give it a refresh, and yeah, it may not look like it here, but I think we actually solved the problem. So notice how the warning has changed. Oh, and by the way, seeing warning and error pages is just part of the job, so get used to it. But yeah, in this case, it looks like within our database file, it's trying to load the PDO class, but notice it's trying to find that PHP class within the core directory, and of course, well, that's not right. That class does not have a namespace of core. Okay, so if I come back, let's see what we're dealing with here.

Handling Global Classes PDO7:58

That class does not have a namespace of core. Okay, so if I come back, let's see what we're dealing with here. And yeah, right down here, you can see we're trying to use this PDO class, but it's set to undefined. And it's set to undefined specifically because we applied a namespace at the top of the file. And the way it works is, unless you specify otherwise, every single class referenced within this file will assume the namespace at the top. So yeah, in this case, PDO, well, it's basically assuming core\PDO, and that does not exist. This is a class that is provided by php.

exist. This is a class that is provided by php. It doesn't have any nested namespacing. Okay, so we solved that in one of two ways. If we ever want to reference something from the global root, that's how I think of it, you begin it with a backslash. So the difference between this and this is significant. This says, start at the root and look for a PDO class. This says, start within whatever namespace you're currently in, and then look for a PDO class.

This says, start within whatever namespace you're currently in, and then look for a PDO class. So yeah, we could update all of these, but yeah, what you'll start to find is it gets kind of annoying, and it's easy to forget. So instead, why don't we just use the PDO class at the top of the file, like so. Come at the top, and I will say I want to use PDO, and you'll see when you define these use statements at the top of the file, it's almost like a little bit of documentation. It's a little heads up of, okay, here's all of the stuff that this file needs access to. So it can be really helpful from that perspective. Okay, so now if I come back to Firefox, I think we will solve that issue, and we do.

Applying Namespaces Across Files9:35

So it can be really helpful from that perspective. Okay, so now if I come back to Firefox, I think we will solve that issue, and we do. Okay, so now if I come back to PhpStorm, let's just update some of these classes. Namespace core, right down here to our validator, same thing, namespace core, and yeah, you would do this for all of your files, specifically all of your classes from this point on. Okay, and then at this point, we would just do a quick sweep through all of our files where we are still referencing these classes in their former locations. So in this case, I can see database and validator. Of course, I can do use core database, and that'll solve it, but don't forget, if you're working along in PhpStorm, let the editor do the work for you.

Of course, I can do use core database, and that'll solve it, but don't forget, if you're working along in PhpStorm, let the editor do the work for you. So for example, for validator, I can press option + return, import class, and again, it does it for me. Okay, so I just go through all of these files and make sure there's no squigglies. That's basically all I'm doing here. About, contact, yeah, these are all static. I think we're good to go here. Okay, so hopefully that all made good sense to you. You've now learned what namespaces are.

PHP NamespacesThe <code>use</code> Keyword

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