Using namespaces with Composer and PSR-4 autoloading

Using namespaces with Composer and PSR-4 autoloading



so if you want to make your own application you will use composer to auto load your files from your directory of choice so here I will paste this code and it says that it will auto add certain directories and it will use psr-4 to auto to the ATP up directory and it will look for the four files there if the namespace of the quad starts with up which happened with a capital letter so let's create this directory and it's called ape yeah ape not it and here I have already included the auto load file so I will want to hear for example controller here so I create one I'll name it and here I have to first specify the name space of the file and it will the name space is the same as the folder so basically it will be a map and up controllers no th two controllers just come to earth open source so next thing is we have to name the cost exactly as the file name without the PHP extension so let's create an index action and turn show list users so next thing is we we would hear some kind of routing where for example users goes to user controller and so on but here I don't really have the time to go with the router so I'll just create a new instance of the controller class user controller and I'll use our echoed user controller index method so it will echo what we will turn here so let's see one one thing is when you have when you change your composer file it's basically you need to run this composer jump out world command and it will make the necessary changes here in the vendor directory in composer so it knows to to add your files from this directory to this all to order here that I showed you in the previous video so let's run it and see if it works yep it works now so yeah that's pretty much it you can make another quest for example let's say you want to have the models folder and you want to have a user model and the user model lives in name space that's it and class name should be user so for example here if you want to get all users at all we could we could call this one just let's make this static so I can call it right away public static it's in the wrong place so let's say user at all and we hear them all and here I have this coloring because it's undefined class imported and here you see my editor just placed use up models user and here's up models user or I could have done research is used and result okay here it's telling me that I don't have a result so you know just returning something return an empty re and our users so we should now print re here yep that's it so basically you can extend and easily you can easily extend your application as much as you want without using Kenya in quotes the only in code you have to do is here in a require render onto what that's it and here I mean here I could have just written distinct right here user and it will work the same in the same way but the difference is that if you use it as it was here above every time you use this user class it will know your action you're referring to this one you could you could also say use this and you will have all all the classes that are here so that's pretty much it hope that has been useful and have a nice day bye bye

2 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *