PHP MVC Framework from scratch | Source code included | Quick programming tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
foreign ladies and gentlemen to this tutorial in this one I'm going to teach you how to create your very own PHP MVC framework okay so an example of an MVC framework from PHP is quad igniter of course and laravel so if you've ever used laravel or codeigniter then you understand what a framework is now we're going to build one from scratch building one from scratch will help you understand how these Frameworks actually behave and work that way you can manipulate them to your own Advantage if need be also going to use object-oriented PHP so it would help if you had some knowledge in oop but not really required because I'll explain everything anyway you can check the link in the description if you want to learn that course all right so laravel or codeigniter we want to make that kind of a framework and the framework is going to use a system called MVC so it's a PHP MVC framework so I'm going to explain what MVC means and how it works so why use a framework in the beginning in fact the first question is what is a framework for those that do not know so if framework is like a skeleton of a website so imagine a already started website but because the one who started it doesn't know what you want to use it for so they just put in all the general things in there like maybe they know you will need some pagination they know you're going to need a routing system they put all that in there they know you're going to need some models to connect to your database etc etc so if framework is just a bunch of code that is already created that you normally use when trying to create a website that way you don't have to write everything from scratch okay so that's what a framework is basically a skeleton of a website so this is different from WordPress which is just a CMS or a Content management system uh with WordPress you need no experience in coding you can simply get it running immediately but if framework needs you to have coding experience because you actually need to code your website it just starts the process for you but you have to finish the coding process whereas WordPress you just put in a bunch of plugins and then it starts running even without plugins it will run immediately but if framework is a little bit different because it requires you to actually code your website so what's the advantage the advantages that you do faster web development of course because the some of the code is already there and then you get a routing system that's good the advantage of your routing system is that things are well organized when you want to add new pages it's easy to add new pages to your website because you have a system in place that tells you how to access those pages there's also code separation because it's MVC and object oriented as well so the view which is the code that you see as a user is separate from the actual code that does the logic so that's why it's called MVC MVC stands for model view controller but I'm going to explain that next then there's encapsulation so all the code is encapsulated like inside a capsule which means it's self-contained and so if you want to move the code from one project to another you can easily do that also makes upgrading your code much much easier so let me go ahead and explain what MVC is MVC is model view controller that's what it stands for okay so model view controller now what let's start with the view the view is what you see so MVC is just a system of a framework a type of framework it that separates the models the views and the controllers so normally you'd have a a PHP page where the top let me use some illustration here so normally you'd have a PHP page like this if you're not using MVC and then there would be some PHP at the top here and then here there's the actual view down here which is the HTML so the user doesn't see this PHP here because it runs in the background and then what they see is just the result of the PHP down here in form of HTML now the problem with this is that when you want to replace how the page looks like you're going to encounter some encounter some PHP code in here because there's all kinds of mixtures in here there might be PHP code at the top but even in the view there there's direct mixing a lot of mixing of logic code and then HTML as well so replacing or changing how this page works is a tedious process because you have to separate the PHP the HTML etc etc the logic you can just throw that away but when you're using MVC you have separate files one file contains the PHP only no HTML in here and then the other file contains the result of this part here so the result of the PHP is sent to this file which we call the view file so this just shows you the result of that PHP now this one that does all the controlling is called the controller so that's how it is so this is where controller and view come in like that so the controller is the actual logic of that page and then the view is the result of that logic now once in a while during control time when the controller is doing its thing it will need to read and write from their database so instead of just going connecting directly to the database it will go through what is known as a model so this is where the model view controller or MVC comes from so this one is a model like this okay great so the model will connect to the DB the database and then it will relay information back and forth here between the controller so I say the controller wants to find out uh how many views this page has so it will ask the model hey model check in the database what's going on how many views are there and then the communication happens and then back to here now the reason we just don't connect directly to the DB instead of using a model is because we want one entry point to the database so that in case for example we change the type of DB we use we don't have to open every single page in our system to change how it's connecting to the DB we're just going to change the main model itself and the whole website adapts to that so efficiency is key here we want to do as little coding as possible right so this is how these guys connect together now what these controllers each page contains a separate controller just like each page contains a separate view so each view is for each page because what's on each page is different so we need new views for each page just like controllers every page has a separate controller as well that acts on that particular page and then in the same way because we are connecting to a DB using a model each table in the database has a separate model so hopefully that is understood here and then there is a router so the user down here let me just delete this so the user will type in a URL here okay URL and then the URL will call up the router and ask hey router which page does this user want to load then the router will be like oh this one wants to load the home page and then it will go ahead and load the home page controller because I said every page has a separate controller so once it loads the controller the controller will run all the logic in there and then load a specific View for the home page and then if the controller and the process needs to read from the database maybe to load posts then it can go ahead and check what table do we want to load from if it's the user table it will load the user model ask it to get some data if it's the post stable it will load the posts model ask it to get something from the DB and bring it back here and then finally show us the result in The View so I hope you understand how the process works now we're going to see it in practice so to get started we're going to need to download a few things so in our case we're going to need a text editor in my case I am using Sublime Text as my text editor but you can use any text editor of your choice and then we're going to need a server and a database now luckily those three those two things the server and the database come in a package in a software called zamp so download this software open the control panel and start Apache and MySQL Apache is our server because we need to copy how websites work online so we're creating a local server here using Apache on our local machine and then we use a database as well so make sure you click Start on both of these now if you're using a Mac use mump instead of xump download bump and install it okay so once you have a server running like this then we can begin to do some cool stuff some coding so what I'll do is I'll go through the folder that I've installed zamp in my case this is inside C zamp and then there's a folder called htdocs which is what holds all my websites every website should be saved in there and then I'll make it specific folder for this particular website called MVC so as you can see I have many websites in here so I just want to drag and drop my folder the MVC folder here so that I can add files and edit from here rather than going to the file browser so there we go CDs okay so once you've installed that and it's running just type open your browser and type the word localhost like this now if everything was installed properly then you're going to see some kind of dashboard here if you see page node found then something isn't running well make sure that you switched on Apache and MySQL in your control panel okay now when you type localhost what you're actually accessing is this page here the htdocs folder okay so when you type localhost you're accessing this folder now so if I go ahead and do slash MVC then I'm going to access this particular folder here that I'm working with so I'm going to do slash MVC now if you're using systems that are case sensitive make sure you type the spelling uh in the correct case if it's capitalized like that so this is the Parent Directory nothing in here which is cool now we're going to put a few files in there so I want to put my some files in here but instead of putting files directly here because What's Happening Here is uh imagine I put some files in here so just say new file and say maybe a set of new files a new folder let's create a folder called the app and let's create another folder called public now if we come back to my browser here you will see that if I refresh it shows me the index of what's in here so the reason I've created two folders in there the public and the app is because I want to have public files that are not so critical to be in the public folder but those important ones to be the app folder now the reason we're putting them outside the public folder is because they are more secure when we do that okay if the public folder is a landing folder of your website then it's going to be very hard for somebody to access a folder that's above that which is the app folder or adjacent or next to it it's sibling folder so that's why we put our most valuable files in there to avoid that security issue okay so now that we have that we can click on public to go in here but there's no firing there so let's add our index file the first file in the public folder and just say index.php that's what I would call it that's the text I'll type in there and that will be its file name and so now if I come back to my browser and refresh automatically the index.php or home.php page is loaded so that's why Apache has loaded it like that so whatever I type here hello world that's what we're going to see in the browser like that okay so there's the file that's working now remember we have this illustration of how things work first of all the user types in a URL then we have a router the router that is going to decide what page should be loaded depending on what's in the URL then when it's fine it finds that page it's going to load the controller of that page which contains all the PHP and no HTML and then depending on how the um the PHP works it's going to load the view which is what the user will see but then sometimes the controller may need to read from the database so it's going to ask a model that is responsible for that particular table to bring information and put it there so that it can generate the view file so this is a basic idea so the first thing we need to do obviously is create a router because that's the first entry point here so that's what we're going to do from the router we'll go to controllers and then views and then lastly models so let's go to the router now and create one okay so we are here now remember that we want only one page to be accessed no matter what we are browsing for so we need to change the HT we need to add an htaccess file that's going to tell Apache that whatever the user types in here just redirect all that traffic to the index file doesn't matter what they type but if they are they are targeting a very specific file you can load it otherwise load the index.php because if I type some gibberish at the end here and press enter it will tell me this file is not found because there's no such file name in the public folder but let's change that so that regardless what we type it will send us to the index page so that we can add a router in there okay so there's some code here I'm going to copy and paste to create our htaccess file so here is the code that I want you to type so type everything as it is please make sure the capitalization is correct do not take any shortcuts so pause the video if you have to and type everything as required anything that has a hash symbol at the beginning means it's a comment so not really important you can leave this out if you want same as this line but these lines are important so this one for example make sure that if whatever the user types in the URL is a valid file name then it should just load that file same thing with the folder so if it's a directory just load it as it is otherwise if it's not a valid file or not a valid folder just redirect everything to the index.php file okay and then create a variable in the get super Global code URL and then put the values of the query string in there so we're going to see how this works in a moment so don't forget to leave a space here and type this there's no space here by the way so don't leave a space here or there so type this as it is so I'm just going to copy this information close that file I also have a robots.txt we're going to look at that so I'm going to right click on public folder new file and paste this information then I'm going to save this file in here and I'm going to call it a DOT so the name starts with the dot dot htaccess okay just like that the HD access is the fire extension so it's not really doesn't have a file name it's just dot HD access so once we do just that one thing so you see in the public folder now we have index.php and we have HT access once we do this this thing that we're seeing not found if I refresh now it'll still load hello world which is the index page regardless what I type in here I can put any gibberish there I need to still load this file but you can see that if I add a new file in here let's say new file just for the kicks of it I'm going to call this one go.php so there's god.php there and inside we're going to say this is go file okay so if I come back to my browser refresh so you see I type this gibberish it always loads the the index page but if I type go.php it will load the go file because this is an actual file that exists so that's what our htaccess file is doing and if we try to let's say mute these guys right here let me come back to this and refresh you see that we remove the ability to even load valid files now we are loading hello world no matter what we do but this is not good because we have CSS files on the website and JavaScript files and we need those to load as they are this is why this section is important without that you won't be able to load your CSS or JavaScript files okay so moving on let's delete our go.php now we put a robots.txt file now what this does is that it tells Google or it asks Google because it's a request it's not a command because the agent can choose to ignore this if it wants so this is just which files or which links to ignore so I'm going to copy this usually if you're going to be dealing with some Ajax thing so I like to disallow this or the admin part so let me close that what we'll do is we right click in the public new phone new file paste this information you can omit this one but I think admin is important this tells the Google crawler to not Index this admin page because no one really wants to be searching for your admin page they'll be searching for maybe the about the contact us page but rarely the admin page because this is just for you to administrate your website so you can tell Google not to Index this page I think even this is good enough like that but who knows to be safe so this says user agent o which means it asks even the Bing crawler the Google crawler etc etc to do the same so I'm going to save this file inside public folder and call it robots Dot txt all right so there goes our security thingies and this is good now that we are able to load the home page regardless what we do then we can do some interesting things like create a router so here I want us to create a routing system now keep in mind that let's go back here let me go back here I want to be able to capture this information that the user has typed and show it here that way I can know what the user is trying to do by looking at their URL so remember that in our htaccess file we taught it to save all that information in a value code URL in the get so you can change this you don't have to be it doesn't have to be URL it can be anything as long as it's a valid key so you can say data or URL doesn't matter okay so in here what I want to do is put my PHP tags like so if in a page in a PHP page you never want to put HTML don't ever put the closing PHP tag just leave it out it's always better that way to avoid problems so we open our PHP tags now what I want to do is simply echo or print whatever is inside the get variable like so yeah so if I now come back and refresh you see that it shows me this data right there which is what you see here but it's in an array called uh in an array uh in the key this is the key the URL key because that's what we wrote in our HD access file keep that in mind if I put a 2 there you will notice that this is what we will get over here okay just a side knot there don't put that to there okay now I want a little function to show me um to show me things very neatly instead of this so I'll do this already function stuff and then I want you to Echo some pre-tags I don't want to keep doing this all the time so that's why I'm creating a function to do it for me like this open and closing pre-tag and then just tell it to print r me what's there and instead of get we're going to use stuff there like so okay so it's just a function to print what I want so instead of doing printer I'll just say show what's in the get like so and it's going to look much nicer so if I do this this is how it looks like I like it better this way so you can see that it's an array and then it's got a keynem URL and that's what has this okay so with this in mind let's imagine we create we have a link like home slash product slash I don't know uh so you see this is what we get in a URL that looks kind of valid home page we want a product and that product is called crisps so this makes sense so what we want to do is separate these Keys these items using this slash so that we can individually have home product and crisps and then we always know that the first one is the page we want to access so home always represents the page the others we can use them as we wish so what I want us to do is put all of this stuff in a value code URL and then do an explosion and say explode I want to use that slash to exploit the values and those values are supposed to be in the get variable URL like that the key with a key URL like so now once we have URL here I want us to Echo it out here and see what it contains instead so URL like so so if I now refresh this is what I get so in the URL we've created home product crisps from this outline here now the tragedies if I remove everything from there I just want to access the home page without typing home I'll get a problem it says undefined key URL because there will be times when there's nothing there and this will not exist so it's going to create a problem so to nip that one in the butt what we're going to do is we're going to do this and say um let's just use URL and say it's equal to get URL but then sometimes it won't exist so we'll put the double question mark to say if it doesn't exist just imagine somebody typed home instead so that let's put that here instead boom okay that solves our problem because now if I refresh it just writes home even though I didn't put anything there so at least now we know the user is accessing the home page when we do that this is what we see so that's the page name the others are debatable so let's create a system now that will allow us to since we have these guys here we we want to use the very first part to look for a controller okay like I have said here the router needs to find a controller so all we need to do is get the name of this page just to name the controller as the same page name that's why it's easy to find it because they are named the same whatever the user types there that's what we try to find so in order to do this let's organize our code a little bit better so I'm going to create a function here instead but it says get or split URL that's what I'll call it Sprint split URL like this okay and then put this code in there like so so this splits how you are instead of echoing it out we tell it to return this data return URL so to return an array that has split that and we can show that by simply doing URL or this is just saying show the result of splitting the URL so we just run this function and put it in there like that okay and we will see that we get exactly this no difference at all okay so now that we've done that we need another function now that will load a page so here we're going to call this function we say function load controller that's up to you what you want to call it load controller like that okay so the load controller page will call the split URL page so that it can split the urls so here I'm just going to say URL is equal to split URL like this okay so this function is calling this one to do the splitting and once the splitting is done the files will be in here and all we need to do now is create a file uh try to find the controller file so here we just say file name is equal to now I want to I don't want to put the controllers inside the public folder I want them in the up folder so right click and create new folder and I'll call it controllers like that that way I can add my controllers in there and let me add a home controller here new file right there right I'll just call this home control I'll just type that in there and then I'll call it home.php in the controllers folder now use a copy tool H there because we want these to be classes and it's a good idea to use capitalized names for classes just for consistency not a must but it's cleaner so over here we have file name is equal to right let's create a file now imagine everything is based on the public folder so that's the base folder so to get to the control lens we have to move your folder up and then controller so here we're just going to do dot dot slash up so dot dot slash is a folder above and then we go to the app folder and then slide controllers slash then now we want to get Whatever item is in the first of this URL so we just concatenate that and say zero whatever the first item is in that array and concatenate dot PHP over there because it's going to be a file imagine we have home at the beginning this is going to be home Dot and then we we added.php at the end now like I said our classes are capitalized here so let's use the UC first function that comes with PHP to make sure that we do we capitalize whatever text is in here so this is our file name all we need to do now is to see if it exists so say if file exists like that and we put file name in there so if that file name exists then we can require that file right let's do require file name that's it so we load that file if it exists and it does exist there's a home.php there and so things are going to work just fine so let's come back here and refresh so we don't see anything [Music] um home we should have seen if it was loaded we should have seen it here now if it does not exist we should put an else here and then we can here you can do whatever you want you can load a file that will tell you um controller not found or something controller not found or just Echo it out but I think we're going to put a file here for now we can't because some for some reason that file isn't found so let's let's see why it isn't if I refresh I don't see any of this stuff there is my home.php what's going on really it doesn't look like any of this is running oh that's because we haven't actually run this function so let's run it here sorry about that we need to run the load controller function so things to work so refresh and there we go so we have home control now for some reason if I type in home 4 which I know is a controller that does not exist then it says controller not found now instead of that we can actually create another controller here let's make a new file this one will be four zero four page not found controller let me save that so here I'm just going to call this one for zero four now because 404 is we can save a file like this but I don't think we can name a class as four zero four so let's start with an underscore because we can use that so say underscore for zero four just because class names are picky let's save that so what I'll do here is in the index when we don't find this we're going to create another path here that we will load and then require that file right here okay now instead of all this we're just going to look for the underscore404.php file instead and load that so back here we'll do this and we are loading the four page note found controller instead but if I type one that exists it does loaded the home controller so from now on all we need to do is add a controller in here for us to be able to load that page so for example I can right click and say new file this one could be the products controller save that and just name it products products dot PHP like that and that's it so if I come back now and change home to products it means I want to access the products page and there it is so this is how a routing system works so up to this point we've created a router so that's how it works very simple and straightforward and then from the home page we can load views from here and models etc etc all right so moving on let's make things more organized because like I said this is going to be object oriented and this doesn't look very oop so let's change that so in order to change things here I want us to reorganize things here so the first thing we're going to do is in every system of ours as long as we're going to be using login and sign up and all that we will not we need to use sessions so let's just make sure there is a session start at the beginning here even though we don't need it right now but you will need it in your project now the advantage of this is the advantage of loading a single page because whatever we're browsing only the index page will be loaded so which means always session start will be on regardless what page you are on which is nice the second thing is we change this to object oriented so let's create an app instead oh a class sorry so we're going to create class named up like so okay so a class for those of you that do not know it's just a thing that can enough for our purposes here it's a thing that can hold functions many functions inside that's all so all the functions that do one particular thing we can just put them in one class that way we don't have separate functions like this and we can always just carry the class to any other project and it will work because it's always self-contained whatever functions it's referencing are within itself so what I'll do here is except for they show stuff I am going to just grab this whole class that is this this here and put it there now when we're using oop it's up to you to choose how oop you want it to be as much as you would like so there are times when it doesn't make sense to use a class but certain times it makes sense to use a function just normal functions instead of classes you can do that or if you don't want to use functions directly at all you can create a static class and put all your functions that are static in there so that you don't have to run functions directly but in our case we make it simple we're going to have both object-oriented sections and just simply procedural Parts but we stick to oop as much as possible so we've put all this in a class now once we've done this we have to declare what kind of functions these are now these are going to be private functions this now the reason they are private is because we are not going to um inherit we're not going to we will instantiate this class but we're not going to use to what's the word I'm looking for instead of inherit with no class is going to inherit these functions so they can be private which means they can only be accessed from within this class now the reason I'm making them private is because these are class is the only thing that will run on this website no other everything rather happens within this class so even though I put them as private it's okay because they are going to be accessed from within here which is not a big deal and then I want to know that a file that will contain functions like this function right here so what I would do is I will cut this function out the show function and go to controllers now in here not controllers but the app folder I want to create a new folder called core this is what will hold all the core the things that need to be always loaded when the functions when the website is running you see this controllers are loaded sometimes and sometimes not if we are on the home page this home page controller will be loaded but this one won't on this one so this is loaded sometimes right but what we put in the core is something that will always be loaded like for example DB control and functions certain functions that are always required will be loaded in here so in the core I'm going to put a new file so we need a few files here the first one is the config.php so we can put our configuration there never put your configuration in other file types except PHP it's safer that way instead of txt for example and then right click new file I will paste my function there don't forget the PHP tags of course at the top do not put the closing PHP tags so this one is functions.php with an s okay so there we go and then we are just going to now the thing is because we are accessing the index page I have to load these files manually because they always needs to be loaded but in here I can put a hundred files now I don't want to litter the index page with 100 files so what I'll do is I'll just put a init file here I will save init.php this is the file that will load all the other files within its folder so what you need to know is that everything in the core folder every file here should be loaded here so let's just do that and say require that's how we load require config.php we start with config because it contains the config it will contain the configuration so obviously we need to load that first and then we may need to run some functions so let's load that save the email init is already saved let me duplicate that we will also need to put a database dot PHP file now this one is a class so I'll use capitalization like that and then we are also going to need model dot PHP and then we're going to need one called controller.php okay goody goody goody these are capitalized because there will be classes now I've put them in this order because model will extend database and then controller will also extend database uh maybe not but doesn't matter we can put them in this order so let me save that now since these files are referenced here we should create them here of course so I'll say new file save this one to be database dot PHP this one will contain the database class new file and let's save this one will contain the punch roller volt PHP file and that's the class and then new file again save this one will contain the model not php5 so you may be wondering why are we creating controller.php when we have controllers in here well the thing is these are separate files and they're going to be classes with separate functions but there are functions that will be common to every controller that every controller should have for example every controller should be able to load a view so there'll be a function that loads that view now instead of putting the function in every one of these classes we just put it in one class this one the main one and then the rest of these will extend that functionality so that we have less code hopefully that makes sense so now that we've loaded everything from the init what we need to do is grab the index file and tell it to load the emit.php file that way everything is loaded here before we try to run anything so what I'll do here after session start I'll just say require a specific file dot dot app slash core slash init.php that way we have loaded all the files in here right there before we do anything down here and then since this is a class we might as well just cut everything from here I'll save this go to core and create a I paste here right so PHP paste there um I will save this as up dot PHP okay now that I've added the app to core I need to include it in the image otherwise it won't be part of the project so every file you add to the core folder you must include it in the init for it to work I'll include the app file last because I want it to run only after everything else is loaded because this is what actually runs the website so this is the up class when we load it it's trying to run this function now this function will not be found because now it belongs to the class and we can test that to see it you say fatal error go to undefined function load controller alrighty then so here we can't load controller anymore we have to call it through the app class because it's part of this so I'm just going to delete that sadly oops rated so that's the app class everything happens within here because this is where we load things are from yes yes now let me go back a bit let me close all these files all right back to here so here at this point I'm assuming the app file has been loaded which means the app class is there so I'm just going to say up is equal to U foreign so once I do this this is how we instantiated class in order to use it so we're instantiating the up class since it exists no errors here but the problem is once we instantiate it we have to actually call this function here so in order to run I don't want to have we can do that by doing this we can say up load controller like that and this will work just fine so we start the app we load the controller which is right here so we can either do it this way or we can add a function that runs immediately we instantiate the class and then that function can call this function here that can work as well but because this is a private function it will not work for us to do this because we are trying to call it from outside the class so unless we say this is public then it's going to work just fine but we can make it public no problem it's not a big deal yeah then if I refresh call to undefined function split URL okay so this one is saying this function is not known because it's trying to find a function outside any class but this one is within this class so we have to use this class to call this so we'll use this keyword like this and say this split this represents this class and then we're trying to run this function here the same way outside here we're trying to run this function that's within the class now instead of app here because we're within the class we just use the word this okay so hopefully that is clear if none of these oop Concepts make sense uh just watch my oop course Link in the description all right so at this point we are still loading things as they need to be loaded you see home controller if I type product here it will still load the product controller okay so that wasn't found because there should be an s on products what have I typed wrong place there we go so it's working just fine home controller loaded just okay so let's recap a little bit here what's going on right just in case you've gotten a little bit lost here's what's happening first of all we've told the website to using the htaccess file to only load the PHP file whatever URL we type you just load this guy once we load this index page we start our session then we require our initialization file which actually loads everything in the code folder now since app is part of the app class it's part of what's loaded we can instantiate the app and run a very specific function in the class itself which is the load controller function and once we load that this is the one that we'll call this function to split the URL and once the URL is split it will look for the first item in the URL and load a controller class from there controller file so now these files like home page they're not classes they're just files normal files so it's up to you if you don't want to use oop you can simply make these your pages and put your HTML here and everything but that's not so much fun now is it because we'll have uh uh it'll be hard to upgrade your system better we have controllers and Views at separate files okay so speaking of that let's add a view folder here so app new folder and let's do use so now we have up views and while we're at it we can also add a new folder called models because we know we're going to need models anyway this is MVC anyway so views models now we want a way to load the views now like I had said in the core we can create a controller class that is going to have basic functionality of loading views so let me put some PHP tags here and create a class we're going to call controller with a Capital C so this is class controller and in here we're going to have a public function because it will be used outside um this class this is going to be called View we just call it that because we want to load a view here there will be the name of that view which is part of the file name and then we'll load it here now the where we load files here will be similar to this so I'm just going to copy this particular one and put it here okay so where are we loading from we're loading views from view folder so we just change the controllers to views we don't need this UC first because there's no need to capitalize The View files and then what I would do is with The View files just so we know their View files we're going to be putting a dot view before the PHP so just say view dot like that okay now we don't need an underscore here because the view file is not a class so let's just remove that underscore we'll have a 404 view file so what we're saying here is that look for the name so grab this this URL is not a thing let's replace that with name whatever the name of the file is just add view.php and find it in the views folder right if you can't find it then look for the 404 View and load that instead okay simple and straightforward now it means we have to find those files here we have to make sure those files exist so I'm going to right click on views new file let's just start with the let me put H1 let's see here page not found H1 like so so in the views folder we're just going to type 404 Dot View dot PHP and Save okay so we have one view file here which is 404 page node found now how do we make the home page or the home controller load the view it wants to load here instead of page not found we can say view file not found okay so in order to take advantage of this function that's in here we're going to go to each one of these controllers and convert them to a class as well so I'm going to say PHP and then say class home now if you notice what we need to be doing is the name of the class should be exactly the name of the file as well that'll make it easier to load the class because uh the file name is the same so this will be gotten from the URL we try to load the file using the URL and once we find it we'll know that there is a class within this file named after the same file name so that makes things a little bit easier so we wanted to be able to use the controller class and so what we'll tell it is to extend it you say extends controller like that okay so what we are saying is that this class whatever is inside the controller class every function is which is here is as good as we've imported it in here so even though this class has nothing I can actually instantiate this class and try to run the view function from within here because of this extension we have imported whatever is in the controller class now remember that the controller class will who will be will be able to extend it because we are including it in the inmates.php so it's always loaded and ready to go so once we say extend it will find the controller class and extend it okay very good so once we have that we are good which means at any point we can create a function here and this one will always have a function called index like that this one will be a public function so that we can be able to call it from outside the class okay so this is the function because every class must have a function we can just put code outside the function in a class every code in here must be within a function unless it's a declaration of some kind but that's why we put at least one function in this case we're just going to call it the index function because that's the first function that always be there that we can create multiple functions depending on what we are doing but the index one will always be here now in here that's where we can do some echoing and say this is the home controller like so okay so if we get this message it means things have worked but if we try to run things right now we'll get an empty page because we are no longer just having code here we have to instantiate this class and actually run it so all we are doing in the app right now is simply loading this file this this one right here but simply loading it doesn't do anything what you can do if you want is just down here you can say home is equal to new home like this okay and then you can say home uh and do this and say index like that and this will work just fine so if we do this now you see this is the home controller so you can do it this way where every controller file will have this code at the end so it can be able to run this now the problem with this is that here we have fixed to running the index function I want the the function we've run here to be dictated by what's inside our URL not just running it like this I want it to be determined by that so if we had coded like this we'll always be running this now the reason I want this is because let's say for example in the home page and we want to load a product so instead of going to the index function we go to the product function so there could be multiple functions here a good example is the crud let's say we or user management for users we have add user edit user delete user right so in that case we can make separate functions for each one of those excuse me I'm almost choking there so the index function could be what displays the list of users and then we can have several functions like the edit delete and update which is edit edit add new delete so we can have more than one function here doing separate things like this and then the URL what's in the URL if there's edit in the URL we know that we need to load the edit function press delete we need to load the delete function etc etc so this is why this won't work very well so let's undo this and do it in a better way so instead there's a what we can do instead of this there is a a function called um core user funk array like this so what this one does is let's do this let me empty that so this param array is just what what things you want to pass to the function this is how you can call a function so I want to call this function here now whatever I put here is the same as passing that data here like for example a B C so if for example in the function I want to pass this data I can actually put those in an array here so for example I can say yeah a is equal to something like that so if I put that and then I put a comma so this is just an array I'm putting here so you can pass an array here if you want but I'm just creating this array on the fly so I can do this a b that's equal to B something so I want this to be text of course so all I'm doing is passing an array here that's all just to show you that I can receive that information here now if I pass two items and there are three items here we may have an error so instead what we do here is we put is equal to give them default values that way in case we don't pass them in here it will still be safe but this is not necessary just to show you that you can do that but then what's important is the function we want to run here I want to run V this function here the index now the problem is the index function is within this class so if I want to run a function that's within the class I have to supply both items so first of all I have to supply the class and then comma I have to supply the name of the function so in this case it's the index which is right there so this is exactly like what I did to call this particular function but I'm doing it like this now because I can later edit what function I want to run here so that's the point so if I refresh you see everything is still running as intended however if I type index 3 which I know is a function that does not exist here we will have a problem you see there it's not a valid callback so these functions are good callbacks because they are given here so in this case we can use what's in the URL to determine what we want to do so since everything is loaded here let's go to the index page in the app [Music] you see the app is the one that contains this URL that can that tells us what's in the URL so this is this would be the best place to actually run this this core user Funk right there it would be the best place to run it because we would then use the variable in the URL to put here here we cannot access it because it's within another class which is this one and this home controller is another class so we're going to just have to instantiate it here within the app.php so once we get here once we load the app.php there right this part is just requiring the file so what I'm going to do down here means I'll do this that way once we load we can instantiate and then call so let's see if that works as well let me refresh and you see works just fine only problem is if I load in a different file that does not exist then we have a problem see that home class not found because that's the class I'm trying to instantiate here so instead of doing this I know the file uploaded and I know what's inside the first URL is the name of the file I have loaded so what I'll do here is this let me go at the top here and just say public or I can say private no problem private value controller so I want to put a default controller here which I'm going to call the Home controller and then here I want to put a method because functions inside a class are called Methods I'll call the default one index like this that way in case we don't find any we're just going to use these babies here so once the fire exists then before I even required or even after requiring I can just update what's in the controller there so just say this controller like this okay whatever is in that controller there I think I don't need this guy there so this controller should be equal to this Okay the reason we're echoing it to that because we have found a file that resembles that so let's just use that as the controller good so that's a controller name great now in case this doesn't work and we get here then the controller name will be different so the controller here will follow the underscore 404. like that that's the controller we want to get okay so we load the file the controller name is the equal to the file name that's why we are doing this so once we do that now we can instantiate the controller because you see here I'm doing home is equal to new home right now because this is very specific to the home page so I'll just call it controller instead I'll say controller is equal to new whatever is in this at that moment let's instantiate that so for example we're on the home page this will be equal to home so we're going to say new home if it's on the 404 we say new 404 and which means this now becomes instantiated a valid callback which we can put here instead of home and then there is a method called Index right here so I'm going to grab this and instead of just putting index there I'll just say this whatever is in the method right there now currently the method is index so we can change that we'll have to change it by checking what's in the URL but for now let's just see if this is working so I'm going to refresh it's not working so it says a class underscore 404 not found why is that why is it even looking for this one so let's just show what's in here I want to see what's inside there so this is what's there okay so it says in page note found but it's saying the class cannot be found which is true because if we go to the 404 page there's actually no class in here so let's copy what's in the home controller and put it here let me grab this message and replace that message here like that we don't need this ABCD I'm just using them for that side and then here we need to name the class the same as the file name like so so that's why we needed the underscore because if I put numbers like this wait can I name can a class name start with numbers let me check if that's actually yeah unexpected integer 404 so it doesn't work so that's why I'm using the underscore there so I was right the first time so it says undefined variable method okay so that's my bad if I go back to the app controller should remove this dollar sign here there we go so now it says unknown named parameter a in the uh index.php where is this up load controller so inside the controller that's where the problem is so it's saying non-parameter a where are we using a we are not using them here let's go to the um where is this in the for it does load this so what is happening what page am I on page four so this goes to the four zero four uh okay so it says up 33 that's where the problem is so up line number 33 where is a being used here at all this is uh weird hmm let me remove these guys let's reduce problems as much as possible so that was the issue anyway uh it doesn't matter we'll fix oh yeah we'll fix that by putting an actual array there no problem alrighty then so this is what we're doing here so now all you have to do is make sure the app exists here so everything in here should be a class so I'll copy this put this in products foreign let me remove these babies I don't need them I'm just using them for the home there we go alrighty then okay so cool now things are running if I put products here you see this is a products controller if I put home that's a home controller if I put something that does not exist 404 control so things are working as they should okay now let's see how to load an actual View okay so if we go to the home let's try right here now it extends control so whatever is inside controller which is this function we can use within here so right here instead of echoing this controller I'm just going to say down here and say uh this View so we're saying this because the function view is contained within this class and it's only contained within this class because we're extending it from the controller the main controller so this will work but here we need to give it the name of the view we are looking for the home view but the home view does not exist we only have a full zero phone which is fine it will load that so this is the home controller but view file not found now let's put our home view here new file I will load the that one and then paste it here and say home page view save this and I'll save it as home don't forget the dot view dot PHP and there we go so if I now refresh so that's how we successfully load a view page like this okay so from here now we can [Music] let's see now okay so what this means is that if you want to make a new page simply make a controller for the page name it whatever you want in the URL to be typed for example home or products and then put a view with the same name and then just put dot view.php so this is a the base framework that we have here so for example on the products page this has a predict class so in the same way we are loading the view here we can do this and load the products view here if you want so and then products like that now since the products view page does not exist we will see a 404 so let's undo and just load the home page so even on the products page we can still load the home view if we want to so if I go to products you see it's still loading the home view page because that's what we've told it to load from here now another interesting thing is that you can actually put your views inside folders so you can say for example products slash products so what this would do is to the path of the view it's going to add a products folder and then slash a file name named products.view.php so here if I right click I can create a new folder in here and just say products and then in there I can do a new file and then save that so I can copy that put that inside here and say products products view file like this and Save so here I can say products Dot View dot PHP in that folder and it's going to find it as you can see here products view file so which means you can actually put things inside the views folder in folders to organize that better now you can do the same thing with controllers okay so for example right now we have the products page here but if I move it to let's say inside control as we create a new folder called Product products and then if I do move the products let me just open this folder and I move the products file into the products folder like this it won't find it now it doesn't really make much sense to put a controllers in folders that depends what you're trying to achieve maybe it would make sense maybe not because each controller is like an individual file but views can be multiple for the same page you can have many views for the same exact page maybe you separate them into smaller chunks so you can reuse those parts so it makes sense for views to be in different folders but controllers not so much but in case you want that if now I try to do this you just tell me control are not found because it didn't find it in there it's moved to the products folder so what you can do is make it in such a way that the uh what is this let's go to the app right here where it says your fire exists you can do another trial if the fire does not exist instead of loading the 404 you can do another fire exists thingy you can copy this let me copy that in fact this whole thing up to here copy this and put it there like so so this is just one option we are not going to use this but I'm just showing you that you can do it this way then you change instead of having just controllers like this you repeat this part right here I'm going to duplicate that and put a slash so that first of all you have the folder with the same file name and then the file name.php if that exists then we load that instead and not the 404 so in this case if I refresh I do get the products um uh page as you can see here okay even if I want products if I remove I still move it from here let's go back to the folder where is that folder here so it's in the products folder I can move it out out back here and delete this folder and it will still work because it checks both sides as you can see I refresh the page even though I've moved the file it still works so you can do it this way but I'm not going to do that just going to I was just going to show you that you can actually organize those into folders aha so from there we are good to go now we have a functioning uh kind of a framework like with this you can actually make your website just put your file here and do that but there's only one problem here uh loading views is weird because of the HT access that we made here so let's add more content here by putting a folder sorry I've said open folder but I wanted to create a new folder called CSS and actually let me make a folder called assets then inside here I will put another folder called images and another folder called Js okay and let me open that folder let me move CSS folder into assets folder so I have an assets folder in the public and then I'm putting CSS images and JavaScript now you have to put your JavaScript and CSS within the public folder if you try to put these guys in the app folder because you don't want people to copy them uh or for security reasons unfortunately that means even the um the client's computer will not be able to access those files because they are restricted only the server will be able to access those files but the nature of CSS files means they are needed on the client's computer and not on the server because CSS files only work in the browser not on the server but in the browser so which means they must be publicly accessible and this is why we added this HT access exception here to allow for CSS and JavaScript files to be loaded still so you cannot protect from someone from copying your JavaScript to your CSS or your images from your from your website so you must put them in the public folder otherwise they won't work so in this case if we tried because you see The View files I'm here in the restricted area so if we try to load um CSS files we may get a bit of a problem because the CSS files are looking at the the actual URL here you see there's products slash products as crisps the image file for example would think you this is an actual folder so let me do a test here in public I want to go to my desktop let me grab an image from here and take it to another folder so in the public folder assets images and paste it here so I'm just going to remit rename it as image just so we can see what's going on so what I want to do is go to my let me close all these files here so that we see what's going on real quick here so I'm going to go to the home.view page this is the home page right now here I want to put an image file and the source is going to be because you always remember things are relative to the public folder so let's say assets slash Images slash image.jpg that's the image really and I'll save that so let's come back and look at our home page here so if I type home this is the problem that you're going to see you see it says broken link even though the image is actually there you see that the image is there if I remove everything but home page there if I do this oh wait actually let me remove all of it oh still not loading hey so there's something wrong here nothing to do with uh let's see did we write the path properly assets images okay so it's the comma here instead of the dot which is giving me a problem so if I refresh you see the images right here right but if I go back I'm still on this home page you see the image is still here even with the text home there but if I type slash and put something else there as in here you see home slash products still on the home page but the image has disappeared if I do this you see that let me just type slash anything here and the image is gone that's because now the image is relative to this slash D and thinks this is an actual folder so to solve this problem we have to put absolute path to the image so here instead I must use the HTTP so I'll say http full column slash slash and then say localhost slash I'm in the MVC folder slash public folder slash assets this is the full path to this image and now notice that I'm using HTTP and I'm not saying I'm not using the direct file name like C drive C slash I'm using the server it must be that way so if I now refresh you see that the image is here even though I put slash many things at the end there as you can see so we can't afford to type this on every single image so instead what we will do is just put it inside a uh what's this a um a constant because a constant can be accessed from anywhere so we want to Echo it so I'm going to do PHP like this and say Echo and we'll call the constant root like this and then close that PHP tag so we put this root and then acids like that so let me save this now we haven't created root but if we go to the config file we can do that there let me put some PHP tags because this is where all the configuration will happen to create a constant you use the keyword Define and then you define its name and then you put its value over here okay now when you're using an online version when you upload your website online you have to put the correct route here maybe it could be https so you put it there and then here you put www dot your website.com don't put a slash at the end just like this so when you move your website to an online system that's where you put your url the URL to your website here in this case our URL is this one so that's where we're doing that so keep that in mind what you can do though is this is what I do personally is I do this I'll say if a server like that and then put a server name here underscore name if server name is equal to and say localhost so if I know I'm on localhost this will tell me I'm on the localhost server so if I do this then I can put an else statement that way I don't have to change back and forth the config file when I uh when I want to edit my website for any reason so if it's localhost I'll put that there Mike so and then this will be because you can only Define something once so that's why we have to put an else statement here we cannot just over you can't override a constant once you've set it so like this you can put your database connection for your localhost here you define your route and then you just do the same for the database connection of your online version and the root for your online version as well this saves you a lot of time of editing whenever you download your website to edit some things and then upload it back to the online version you won't have to change anything in your config file so with this now we are able to use root here and let's see if that actually works if I refresh nothing has changed it works now keep in mind that PHP Echo can be replaced by an equal sign just like that and it will still works exactly the same you can even remove the spaces here so it's nice and simple like that and it still work Okay so we've fixed that problem by putting root so every time you want to load an image even a CSS file or a Javascript file make sure you put root at the beginning and then slash assets slash maybe JS and then the file name so keep that in mind so now that we are done with these we can move on to something else which is the models so we have views we have controllers we need models now so like I said just a recap if I want to put a JavaScript file here or a CSS file I need to follow this process as well so here I can put maybe a link like so and then here where it says href I have to put root assets and then output CSS here because that's where they are and then maybe it can be style dot CSS like that so this is how you load your CSS files and even your JavaScript files do not forget this root here very important so just to a reminder there and also and now we can go to models so we need the main model here which will extend a database okay so in database here what I want to do is I want to create a class in here called class database so I want to start with that because models has essentially just classes that connect to the database so let's begin by connecting to our database so before I even make a class I just want to run a connection right here so do con like this connection is equal to now for a professional um MVC thing we have to use PDO because yeah because PDO is just better so let me add a string connector here now with PDO you have to tell it what driver you are using I'm using MySQL put a full colon and then put your host name there in this case this is localhost and then you put a semicolon there and then you tell it there um what do we tell it here what else is there DB name yeah so what is the DB name so here we don't know the database name but you can always omit this part for database name you can simply add it on later to use it there but if you already know what the database name will be let's put it there so here we're just going to say my DB that's what we'll call it my DB and then let's put that string in here but then we have to put username which is root and the password is an empty string now if you're using mump the password will be root as well just like the username so I just want to know if we made a connection here by doing show con just show us the connection is it a valid PDO thing but also we don't really have this database yet so if you want you can actually omit this database part I'm not sure if this will work with mump but we can test it so if I now refresh call to undefined function PDO now it's looking for a function but instead it should be looking for a class and that's because we forgot the word new like that to tell it this is a class so as you can see now it shows me PDO object which means it actually works out now if I undo and put my DB name there don't forget the new here again now that DB does not exist I'll still get an error that uh unknown database so which still means things worked out so what I could do is name put the database name there where is this anyway this part doesn't matter because we we've seen that it actually makes the connection so that's that's fine this is all we needed to see so instead of putting these variables directly we're going to go to the config file right here on the localhost we're going to do some more defining so I'm going to say Define instead of root this will be DB name so here we're going to put the database we'll be working with we'll call it my DB and then you can put some commenting like this and say uh database config like that okay [Music] and then let's duplicate this so This DB name is DB host this is localhost in our case then this DB user and this DB password Here so in our case the password is empty the user is root so put if you change these values in your MySQL you asked to enter a password and username use those instead so yep I guess that's what we if you want you can change the driver as well that's up to you let's put driver maybe you want to use postgres database instead you can do that so I can copy these babies and put them here that way when you go on your online website you can put your stuff here instead okay so we have that we have this that's good now let's just make sure that my DB actually exists oh before we do that let's get back to this part so since those are constants they can be put anywhere so like in here I'll replace with a constant which is DB DB host oh my bad sorry let me just copy that and then do this is not the part that you change it's this part here so that's a DB host let's change that part as well to represent now we don't need that last one then uh DB name and DB user I don't need these dots here anymore foreign pass [Music] okay cool so with this we'll get the same the same message again so now we can go to our localhost let's say localhost PHP my admin which is where our database resides now you must make sure that MySQL is running otherwise this won't work so since we are here on our databases we can click to create a new database and say uh my DB and just hit create and we've created our database and then that error will go okay cool so now the PDO object is valid now we don't use it's only showing because of this if I remove that then it won't show anything there now uh what we would do here is because we are using classes we can do a class database like so class database capitalize like this okay and because we need functions inside classes so we say functions um what do we say functions connect this is function connect this function will always only be used in this particular class or call it private and then this is what will create the connection like so right and then instead of just doing this we'll say return connection now maybe just for readabilities sake let's do that instead so it's easy to know what's happening so we create a new PDO object we put it here and return it as a result of this function alrighty then alrighty then from here now we can create some public functions and call this function let's say query just to run a query from here if we want to public function query this function will take a query and it will take some data in form of an array so let's put an empty array because not always we'll have this I'm doing this because we want to use prepared statements prepared statements are safer because they prevent SQL injection normally when you write a query you put your variables inside the query but because usually the variables you put in the query come from a user form the user can manipulate your query using those variables so instead of typing the username or password they will type a string that will manipulate to your database also known as SQL injection So to avoid that we use prepared statements where we Supply the query separately to the variables that way the computer knows this is just the query and these are just variables they are not part of the query so I have security I have videos that teach security you can check those out on the channel so here we have this and all I need to do now to create a connection is to say con is equal to this connect okay so now it's calling this function to run a connection and then we can run something now once we do this we need to prepare a statement so call this one statement is equal to connection prepare and then we prepare the query itself like so now once we prepare the query then we can do statement let's see if we check we just call it check because we are checking to see if it will execute now during the execution this is where you supply the data if any is available so let's put that there then we'll just check if uh is if the result is an array and at the same time these count of check actually instead of check maybe we can just call this result it's easier to remember I guess Okay so this just shows us the easy result there let's return the result actually this is not a result eh I think check was better because I want result after this so [Music] yeah actually not true this one let's put it at check I missed this step worry about that so say if check then let's get some actual results now say yeah statement fetch all now this is where you can tell it what you want to what kind of items you want to fetch you can use the constants provided by PDO by doing that and then you can put fetch and then here you can put objects which I I like or an array like this so depending on the result you want you want an array that's what you do if you want objects you do this I like objects myself so I'll leave it there then let me duplicate that remove this one so that this is inside foreign if the result is an array and return the result otherwise if it gets to this point we just return both because things didn't work well now it's not always that when we return false the query didn't run because sometimes there are queries where we get no results like for example an update query or a delete query we don't get any results back so it just returns false but this is our query function this is how we run queries so we can test this by running one query here now I don't want this to be a class I want this to be a trait because somebody told me I should show how we use trades as well so we'll call this a trait instead okay now if you want to make it a class the way it is right here what I would use these four is if I go to model for example and create a class named model actually if I use this name model clasp so you give me something nice here so I'll call this one model and this one extends database just like we did with the others and this is a Constructor here or let's just say function test that's why we'll call it function test and in here what I'm going to do because it extends database it means everything in database here I can use so I can run this query function so if I want right there I can run a query by just typing a query and say query is equal to and then I'll just say select or from users now there is no users table so maybe we should create that very quickly let me come here create table so click on the database you want and then create let's create a user's table number of columns we leave it there let's put an ID do this ID name age and maybe date so this one is what's this date time default value current timestamp age we leave it at integer name variable character maybe 100 characters this will integer put Auto increment to make sure it's primary key and say go so now we have a table this is just for demonstration purposes it's not permanent and let me go to insert so I can add a few records so I just need to add a few people and then let's put John and let's put an h and hit go so now if I it shows you the query that it will run in order to create that which is nice about this so if I go to browse you see there are two records here which is nice so now if I try to in the model I say query and then I can eventually do this and say result is equal to query oh sorry this query like that so I can do this because database is an extension or model so then I can put query there like so and then I can simply show the result so in order for this to happen we have to instantiate model and run the function test so we can do this anyway since we are in the home controller we can do that in the home controller here just to show that things can work like that so if I ever refresh that's the home controller before we Echo all that what I can do is I can just say model is equal to new model and then I'll say model test like that okay so that's how I'm running that function there and if I refresh you see it shows me two results there's a record here and another record there so things are working as intended here but there's another way of doing things instead of extending database we can change database to a trait instead so let me change that to a trait so I'll show you the differences once I do this I cannot extend the trait anymore so if I tried this it wouldn't work so class model extend straight database class model connect extend the trade that's what it's saying so let's remove the extension so how exactly do we incorporate that we say use database like this so once we say use database that's how you add trades to these things instead of extending you say use here and it's going to work now and there we go everything works as intended so you may be asking why use this instead of the extent well extend can only extend one thing at a time so if I say extends database like that I can't do another extension like this this is not going to work you can only extend one other class at a time but there will be times when you've created multiple classes that you want to include within another class so that's when you set them as traits instead of classes that way you can say use database and then you can say model John so if all these were traits you would use them like this which means every functionality from each one of these is now part of this class as well this is how you reuse code in a smarter way without having to retype it in every class so this is quite handy actually anyway use database Q the only problem with the traits is that you can't instantiate it directly you can't instantiate it like this so you can't use these guys without using model so you need model to use database because a trade cannot run on its own unless it's used within another class but that's fine so inside model we need to create all the functions that are common to every table whenever I want to access every table there are common functions that will be here so that when we add individual models here for each table then we can use reuse the common functionality in here so let's see what common functions we would need the first thing is so this will probably be public functions because they will be used outside mostly so public function number one we already can query right but we need to be able to insert okay let's duplicate this a few times so insert we need to be able to update a row from that table so in set into a row update we need to be able to delete a row okay so these are the operations insert update and delete to read we can use a query which is already created from that database class now we can move it from the database class to this one and simply leave the connection in there but I think it's okay here it's up to you you can move this query function to the model itself if you want it will still work exactly the same right so [Music] um let's see should we do that maybe we should I think it makes more sense so let me remove this we just leave the connector with the database here and let's put it here so I'm going to replace this with the query function like so so public function query and we can say this connect because once we use database it's as good as having this function in here as well and sometimes we just want to get one um one record from the query result so instead of query we'll just say get row like that you can name these functions anything you want okay so we just get row the only difference with this one is that this one will return just the first result like this okay otherwise everything else Remains the Same and then I want another function called first actually you know what hmm beginning to doubt this let me move these actually both of those back to database sorry about that yeah because database is a trait whatever um at least it should have a minimum of querying at multiple queries and one row at least so that when we use this trait in another maybe we want the controller to have the ability to just query directly then we can just say use database in the controller or in any of these you can just say use database products use database and then we can run whatever is inside database directly without having to deal with a model so that's up to you because there will be times when you don't want to deal with a model you can do it that way okay so uh wait let's get row here where is model okay so we don't need to get raw anymore so here what I want is one more function code first okay so these are the functions that will be inside model first is going to return one row just like we did with get row here this returns one row but there will be a big difference because here we put a query but here we want to we won't be typing queries instead we just be putting values the queries will be Auto generated so you'll see that same as insert here we just put the values we want to insert and it will create a new record for us we don't need to write a query itself just like update and delete now these params will get here will be we'll need an ID for the update and the delete actually the delete only needs an ID so if we give it the ID it will know which row to delete based on the ID okay now there may be sometimes you don't want to use the main ID you can call it you can put another one called ID column like this so the default value is ID now if you don't want to use the main the ID column if your columns is not code ID the one that you want to use to search for which which row to delete you can put in a different one here same thing here we have update the ID we want to use and then from there we will put in the data that we want to update right so the data cannot be empty and then just like here we can put a different ID column if we want like that okay if we don't want to use the ID column if we do we just leave that b in the inset all we want is the data we need to insert we don't need to know what row number it should be that would be Auto generated the first actually there's one more function missing which is the where function okay so this one we just need data just like this one we just need data so these functions are exactly the same only that this one returns one row while this one returns multiple so all we need to do now is create a query here and then use the the query function to actually get a result from there because the query function requires a query and some data okay so cool so the way I close is for getting results from a specific record so let's imagine we want to run a query and the query is like select from or from users where the ID is equal to ID like that so this is a valid query only that ID could be a number like this one okay so this is a valid query the only thing is we want to avoid putting variables actual variables in here because we might do something like this and whatever is inside ID will be part of the query but like I said because of rescue or injection we shouldn't do this because you could have SQL injection instead would do something like this and this tells PDO that this is a variable okay and so it means we should provide some data actual data later so the way we run this query is by doing this and say this query and then we put our query in there which is this text at the top but then we have to put an array here because we are using a we're telling it that there is ID somewhere we have to put an array that has ID with an actual value like 23 like that and so when this goes to the database it's going to run this query which this function which gets a query and some data and this data will be injected during the execution after the query itself has been prepared so that's how that works so what I want to do is that from just this array I want to be able to create something like this so we're just going to say query is equal to say select all from users now this table uses is very specific what if we want to use this model for a different table so what we will do here is we're going to put a protected value I will say protected sorry here a small letter so protected this looks like public but it's just like private but inheritable so don't worry about that you can check my oop course to know what that does now what we'll do is like I said each table database table will have a separate model and each model will have this value which tells the PHP what table the model represents so I'm just putting a generic value here but this will be overwritten by specific models and their actual tables so in order to access this we're going to do this let's say this uh table so whatever is in here will be put there so selector from this table where there instead of saying ID is equal to ID we're going to use the data from here to do that so what I'll do is I'll just grab the keys first from that data Keys is equal to there's a function called array keys which we just grab all the keys from an array so from this now I can implode the keys to do this because where this is equal to that and then if there are several of them we'll say and Maybe date is equal to now or something I don't know but when we have multiple values here if we have a single value then we just say ID is equal to ID and then if we have multiple we say and just like that okay and this is that now we can use the where clause and put also data not like that now data node is going to be the part where it says instead of if we have two values here this should be translated as maybe ID is equal to these and date is equal to this and they need to grab what's in here and say and maybe ID is not equal to whatever is in that particular ID that's in here so this is for negation this is for what you are looking for this is for what you are not looking for okay so we can do the same here for there because this is literally the same function just that it returns one result here okay so um what the part we need to create is this part using just the data from there so I forget about this for now I'll try and use that so we grab these keys and import them so just going to say Keys string is equal to uh actually maybe let's do a loop instead [Music] should it be a little I think it should be a loop so we'll do four each keys as key and then here I'm just going to say a string for Str is equal to let's start with an empty string and then update it as we Loop so whatever the key is we update so here we're just going to say Str is equal to the key name and then we concatenate with some text equals that and then the key again so what I'm simply doing is trying to recreate this so key which is let's say the key is ID and then we put the key again here after this so just like ID is equal to like this ID is equal to variety so this is what I'm trying to recreate here but then if there's more than one there should be an and at the end so we're just going to concatenate the end like so put some space like that okay so that it repeats as many times as we need and then we can also do a for each Loop for the not version so this one key is not so just like we've done with this I would duplicate this and do keys not which will use the data not and exactly the same thing instead here we'll say not equal to maybe I should leave a space there just yeah and this is not equal to and and and then at the end I just want to remove the and that is at the end because I don't want it hanging like that so I'm just going to do string is equal to trim um let's trim the string and then we give it what we want to trim and that space and the ampersands like so so it would trim all that out and yep that string now actually maybe instead of string I can just put query here so what I'll do is I'll change string to query even here as well query and then do a string is equal to here we are concatenating so let's put a dot and a DOT there we are saying add to that's what the dot equals mean let's put query here as well then start the query with this part up to here where it says where so let's put that here so selector from the table where that's why it's the where function and then we add the where part here up to here we trim that out and then we add um what we add here now here I want to put another protected value just so we can have pagination as well I'll say protected limit and offset foreign I'll show you how to use these for pagination so offset is zero limit would put it at 10 results for now and so here the query now will concatenate after everything we'll say limit leave that space there and then we'll say this limit okay and then offset and then we'll say this whatever is in the offset of that like so okay and then we can run the query but before we run the query I just want to show you what we've done here so I'm just going to say Echo query that way you'll see what's going on all right very cool so let's try and use the where function I'll go back to my home controller right here so we have a model is a good new model and here I'm just going to use the wire function and if I come back the wave function the knot is supposed to be optional so I'm just going to put an empty array so that we don't always need it so back here in the where Clause I want to select an item with an ID of one so I'm just going to supply an array with a key named ID and then put the value of the ID this is the same as doing this creating an array and say is equal to array like that array ID is equal to one like that and then I put the array in here like so okay so model where now what I want to do is just say result is equal to yeah let me remove this here then all I want to do is show the result so just ID is equal to 1. now let's see what this does so if I refresh unclosed home.php online 10. so there's something unclosed on line 10. Open Bracket oh I forgot that sorry about that refresh and there we go so you see this is the query we've created selector from users where ID is a good let me leave a space here as well so we've back to model I can redesign this by putting a space here yep better okay so ID is equal to ID limit 10 offset 0. very nice now imagine I want to use more items to determine what I want so instead of just the ID I want where the ID is one but also I want where uh where the name is equal to that okay so let's see if there's a record like that so you see this is the query we make selector from users where ID is equal to this ID and name is go to name limit or offset Okay cool so we're going to give that information here but let's see if it will actually get a result so we are using the way I close but it's not actually running anything so instead of echoing the query oh wait uh let me come back here let's see if they're not as well works as intended so I'm just going to say put another item here and say name is equal to John but I want don't want the name to be John so I'll put this array as array 2 like that and then put it here so what that should do is create a query where it says where ID is a good ID and name is going to name it and name is not equal to name so this won't return any result but you get the idea here we can have instead of name we can have name two or another column maybe date okay and then just put the date function there like so uh maybe just the year but we want so let's see that query we've made and there it is so ID is equal to ID name is equal to particular name we've given and date is not equal to that date we've given so you can make a complex query from this not too complex but good enough so let's see if it will return a result here let's remove array two just use that array one come back here and actually delete this and try to run the query so say return that's what we're saying this query then run that query and then whatever data was given there so we'll say data and data not yep [Music] oh we don't actually need data not that's oh wait we may need that yes we Hmm this is interesting because the query function in database only takes in one data one piece of data so what I could do is merge these two right so I'll delete that I'll just say data is equal to array merge that's how you made two arrays output data not there and the data here that way they're all in one value and then put them there okay after because we separated them just so we can create the query and know what's what but then the variables should be mixed together after that all right so let's see if this will return an actual result and there you see the ID is equal to 1 with that name there so let's see if I wanted to the home controller I wanted to get a result where the name is John then I'll do this and you see there it Returns the result that I want very cool so that's the basics of how we're going to be querying things the only difference with the other function here is this so this is exactly like this the first the only difference is that here let me duplicate the last one instead of return I'm just going to say result is equal to and then here just say return let's see return false but then say if results meaning it didn't return false then let's just return the first result here okay so with PHP if it's only one line in the if statement you don't need to put the Kelly brackets so this is valid so if the results are returned if you just return the first one if not it will return false which is what this will return anyway in the beginning so there we go so we have two functions done the way I closed first now we need a function that can insert and the function that can update okay so here we just the insert data so in the same way we want to create an array so let me come back here to the home page let's try all these functions here this one will try the insert function so put an array here of the name and the date for example because really our table only has name age and date so let's put H there as well actually we don't need the date because you do auto add so let's just say 45 and we want to add a new record for Peter okay like that and then insert there will be no results here so no need to show any results model instead actually you can just leave that b so how the insert function work well it's going to be very close to this only that let's grab this Keys part we need that so like that and then even this query where it starts from we need that so let's put that here now an inside query is like this it says insert into whatever the table is this table and then here you put two brackets and then you say value values and then another bracket like so insert doesn't have double e so this is our insert query here we put the names of the columns here we put the names of the values now since the values and the columns are the same since we're just putting variables here not actual values this will be easier to make so here all I want to do is create something like name comma H comma date like that okay and then here we'll just put four columns like this because the values will be provided separately so this is how the inset query should look so I just want to create this part based on this data from the keys so here what I'll do is I'll just use an employed so I'll just do this say implode and then I will put a comma so import will crash a an array into a string and whatever you put here will be the separator so like you've seen we are separating with a comma so just import whatever the keys are okay so I'll put that in here now do exactly the same thing here the only difference is that these guys should have a DOT at the beginning so that's the only a full colon at the beginning of the text so instead of just telling it to separate by this it can put a full column after the comma like that though the first one will also need a full column so let's put a full column there and that's about it really so insert into table blah blah blah this is a query so I want you to see the query here let me just Echo the query before we try to actually run it foreign this is what we want to insert let's see what it has done what query it has created so if I refresh this is what it has said and set into users name and age values name and a very simple and straightforward so if I added more items here let's say for example the date it's going to reflect that so say date we just want the year maybe the month and the day like that so let's do that and then we're going to see name age date and so on go the values we can't see the values but they're there so I'll remove that let's see if we can actually add this and let it run and see if it worked so here to make it run we just run the query so running the query is like this copy and let's put it here cool let's return false so this query we're not expecting any results so we're not putting a variable here so query and whatever data is applied we put it right there that's a simple inset query so let's see if we can actually insert something so in here these are the records we have two records let's see if we can add a third so refresh come back let's check it out and as you can see we've added Peta 45 and it worked so the insert query as well is working let's look at the update query now well let's do the delete first because that's easier so we want to delete this third one with ID number three okay so this one is easier we're just going to say delete from whatever this table is and then you put a where clause so this really follows the the way we did the where function here so I'm just going to copy all of this and put it right here because actually that's uh so this is a delete so we're looking for the ID so instead of all this we're just going to say selector from that where actually I didn't need all of this so thought I needed it but I do not actually don't need this either do I need this array merge I don't need that either I don't need to return anything either here okay I don't need the keys not either so here you'll say keys is equal to select or not instead of Select we'll say delete from table where we'll say let's just say where ID is equal to it should be like this okay so it's going to be equal to ID whatever is in there now if the column let me remove our ATS here all I need to do is just say uh data and then let's use the ID column which is ID in this case it's going to be equal to this ID we Supply and that's all so we put that in the data that's what we're supplying there so instead of where we don't know what column name will be used so we grab this and put it here this is what it should look for in the array and we named it appropriately with uh oh wait a minute uh the ID column wait wait wait wait wait where the idcon let's say we type where name is equal to and then we put a name value there so this will be name in there so where that is equal to okay so which means I have to repeat this here the column name because that's what we are using there okay so it should be like that instead so let's test this out before I actually run the query I just want to Echo it out and see what's going on and then mute this so if I come back to my home page instead of insert I want to delete now I want to delete using ID as a column name the ID will be oh actually I don't need this what I need here is just to provide the ID name so here I want to delete record number three so I just need to provide that but if I don't want to use uh ID as a record now maybe your database is has user ID ink in that case you're going to do that instead so let's see what this does before we do that come back here and refresh and this is what query you get delete from users where ID is equal to ID simple and straightforward but imagine I put user ID instead it will read like this delete from users where user ID is equal to user ID with the user ID being provided as three so let me remove this let's try and see if it'll actually work let's go to model let's remove that let's remove that so let's see if record number three will be deleted because it's right here Peter so if I now refresh we should have it gone and there it is it's gone so the delete function is working as intended let's look at the update function now okay so this process takes a while especially the model part but once you have this it's very powerful because you can do things very quickly by inserting updating without having to worry about typing queries so I think the update function Works similar to the the where function here so I'm going to copy that where function and go to the update and paste okay so there's an ID here and an ID column that we may need so this part will work the same as here so um wait we just need an ID right okay okay let me paste here so say data and then ID that's cool and what else did we do here we'll use the same where close so copy that and where do I put that I'll put it here instead of these offsets and stuff so say where are IDs go to ID I won't need this merge so let's remove that guy I won't need a return we're just updating so let's return false this one will just run without anything like so okay so we have our data that's cool we have this part we don't need this part I think or maybe we do I will leave it there for now so what I want to do is look at the data that okay so this this shouldn't be named data because I'm replacing it here so that's not cool so instead of data we're going to call this one array oh wait hmm no no no no no I think we need to marriage this is the thing here so data comes with its own things except for the ID so instead what I'll do is I'll leave this actually whatever data comes with I will use the column name and set add ID there okay so that's fine I guess so uh updates the query goes live updates we don't need this note update the table and then we say set then let's say ID is what you want to edit your name you say name is equal to and then a name and then a comma and then you can say date is equal to and then date Etc so this is the part we need to create in these Loops here we only need one Loop we don't have the knot so to set and then we do that so I'll leave it like this so here we'll get the keys from data and put them in the actually we should add this after this process because we don't want the ID to be part of this update process so let me put this right about here just before I send this data there so set Keys set so we Loop and say key is equal to that and then instead of this we put a comma instead key is equal to that comma so once we are done with this we can try to delete the comma and then where I think this works as intended so what I'll do is I'll just Echo the query and see if it's correct before we try to do anything so here we'll say update so I want to update where the ID is equal to 3 actually there's no such record where the ID is true I want to add Mary as the name so I'll put an array here instead and then put the values in an array so say array name is equal to Mary let's change the age as well so I'm going to duplicate let's update the age to 50. and let's put 50 here so we want to update the name on the edge of record number two so let's see what record number two is it's this one John 34. so let's see if we can change that if I go to the home and then it will update blah blah blah nice so let's try and refresh so it's complaining about an enclosed bracket online 90 in model so let's go to model 990 okay so there's that which we do not need let's refresh this is what we get update users set name is go to name and the age is going to age where ID is equal to ID so this looks fine to me so if that's true then let's release it to actually run so back here let's refresh and let's see if John has changed to Mary and age 50. and there we go Mary 50. so the update function as well is working so everything is working now we can delete we can update without and insert without typing any query you can grab one record we can grab many records without typing a single query so let's see uh how we can utilize this in practice right so let's separate these a little bit like so now in order to use this we want to be able to we don't want to actually have this model be used by itself like this so we're going to change this to a trade as well so let's change this to a trait so I'm going to put trade here instead trade model like like this all right cool so then we can remove this protected table users as well because this is oddly specific it's very specific to a specific table but the model class or trade should be agnostic it doesn't need to know what the table name is so let's remove that that's for each specific model to know so let's leave that out now what we want to do is we go to models here and let me right click and say new file here I'll do PHP like so and then put a class in here so let me save this as user so dot PHP now models should be singular not users but just user like that and then now I can put a class but you know those are just rules use a class they are not lows so you can follow as you wish and then it shouldn't extend any class normally if the model was a class we would say extends model here like that okay so what would say if you leave this as a class you can do that if you want if these traits are giving you a problem just leave it as a class but the reason I'm doing this is because I don't want the model to be instantiable because this would not work on its own because this table is empty this part this table should be very shouldn't be very specific here so it's better to leave it as a trait hopefully that makes sense but if it doesn't don't worry just follow the instructions here here we're just going to say use model like this that's all that way we import the model so every class should use model like this and that's it so here all we need now is the table to tell it what table we should be looking for so we say protected table is equal to users this is all we need for the actual class another thing we may need is another protected variable uh which I'll show you what why this would be useful would say allowed columns is equal to and then let's do that okay and then in here this is an array and then we can tell it what columns are allowed to be editable so in this case we have name and then we can have age like this let me put that in here like that so you can put all the columns in there now this is particularly useful if for example you are you have a form for a user where a user wants to sign up right now there are a lot of inputs there some of them you want to save some you don't for example when they tick to say I accept the user agreement there's no need for you to save that that tick in the database it's just there to tell the user that they should accept the terms once they sign up but when you click post the post variable will contain that tick or accept terms and conditions right but you don't want to save that in the database so if you have a list of allowed columns that can be edited it doesn't matter if you give it the whole post variable it will eliminate those columns it doesn't need and only leave those that are required and save those this is why this part very very super useful this right here so this is what you need for your for your actual model class now like the user's class the rest of the functionality is inside model and database which makes this one very small and lean then here you can add some extra functions that you want to complement the user class if you need B so we have one model at least so this table is what it will get when um when doing this when checking for this table that's what it will use so here in the home page you see instead of instantiating model we're just going to be using this as a user whenever I want to write to the user's table or right read edit delete will do this we'll say user is a go to new user then here we say uh let's just add some new data right this is some new data I want to insert so here I'll use user like that I'm not expecting any results so that's fine I'll just remove that and in set and then I'll just insert the array like that so here as you can see I don't have to specify which table name I'm doing as long as I instantiate the user model it knows that I want to insert in the users table so let's see if this is going to actually work so back here if I refresh so class user not found okay so great it can't find this user class I'm trying to instantiate now the user class we know is in here in the models place um and it's named exactly as the name itself the name of the file is the same as the name of the class which is nice so what we can do now is create an auto load so we'll go to the emit right here right at the top there this part will check if a class is code that does not exist then it will load it so here we're just going to count the function is called SPL autoload register so this is the function that loads whatever class you want that you can find so this thing takes in a function like this now the function will receive a class name so here let's just see Echo class name so this function is only code this function right here is only code if PHP tries to run a class and it can find it instantiate a class and can't find it it will run this function instead and tell you what the class name is that it's trying to find so if I refresh you see it shows me user so what I can do with this information is that I can just say [Music] you see first just to make sure it's capitalized because class names that's how we want them to be so I'll create a file name here and say is equal to the class name but I want to find it first so go to the app folder and go to the models folder and concatenate this name and then say dot PHP so you see file name there in this folder the PHP so let's see what it will show us instead so here's the file name we got up models user.php so I think we can find this so instead of this we just say require the file so we require that file and that's it so back here if I now refresh you see we don't get any more errors because the file has been found and worked let's see if you have added a new record and as you can see there it is Boom 60. okay which has happened within the home controller so this is what we will do if we want to read from the database so from now let's see here what am I trying to do um insert insert so if I want to do let's try the other uh let's try the other functions let's try the where close so I just want where uh ID is equal to 2. let me remove this so user is going to new user result is good user where ID is equal to ID let's see if we get some results there so it's working just fine now we can use the web close let's try uh we can put one more function with [Music] code defined o right let me put it down here let me duplicate the whale clause just to get all results so this one we can call it find all you can call it anything you want that's just me so instead of any of this all I need to do is run a simple query doesn't need any data so say select all from this table actually something I forgot here the order so we can say order the order should be in descending order and then we can also put order column or the column will be ID okay so this will help with this part we can put order by this order oops what have we done so that's the order type let me do that say order type so the way it goes is something like order by ID descending so first of all we use the column and then they type so this will translate to something like order by ID descending if it's left as it is right there so this will help we don't need this on the first we don't need this there we do need it here yep so the order by comes before these limits and all that okay great so this find o should return all results if I refresh here uh let's go back to our home controller here instead of this we just say find oh and we don't need to supply anything there because we're just getting all results all the records I mean and there we go so it says on Divine variable query model line 18 where is that oh it should be equal without that dot there we go all right there we go so it returns all the result through 10 10 records at a time because we have pagination activated so we can close the model uh thingy there and the database as well the home this is what we've got very cool so now if we want another model like for the products table we just copy exactly what's here and then put class product instead of user and then tell it what table the products are found in and then put all the columns that are that should be editable in here as a list this is all you need to do now the reason we need this is so we can trim when saving so let's do that right now actually we need to do that in the models we need to do that when in setting and when updating so before we can do any of this we have to trim down the data that comes in to leave only the required ones so we're just going to say if uh not empty okay this allowed columns so if that part that variable or that array is not empty what we'll do now is do it for each Loop of this right so we say no actually we we look through the data that's what we do we look through the data and check if any of the keys from the data is not available in this allowed columns so we'll say here if not in Array this is how you check if something is not in an array the needle is what we are looking for we're looking for the current key of this data and then the haystack is where we want to find it which is this allowed columns so if it's not in there then we have to unset it and say unset data and the current key that way we delete that item from the list of data because it's not part of the allowed column so you shouldn't be part of the query so here you can put and say remove unwanted data so let's copy this we should put this also in the insert like so okay we have to clean out the data as well okay so that does it so close model the PHP now everything is cool so what I want to do at this point is remove the products that PHP delete that I just need the 404 on the home just like this folder here products delete folder okay I just need the Home View and the file not found let's see and then one model user.php and yeah the rest are good so if you have any functions that you want to add to your uh your MVC this is where you add those functions for example you may need a function called Escape which is very useful you put a string in there and then you return an escaped version of this which is uses the function HTML spatial charge and let's string there so it's just used to escape some JavaScript so that you are not attacked because somebody can instead of putting their name in the name field they can put some JavaScript and then when you Echo it to your page it will run instead so we use this HTML special charge to mute it so that it doesn't run as JavaScript okay so let's see what file what else we can add here so in here we put all the configuration so we can put other definitions like Define I want app name app name here this one is my website as a default value you can also put other constants like app description [Music] best website on the planet okay so you can put constants and whatever values that you want to be used throughout the file this is where the configuration is you can configure things like we're in debug mode or not for example you can say Define let's use debug like this and then you can set this to true or false so we are in debug mode which means we are in production so this will show uh errors we can do this say true means show errors false uh means do not show any errors so you change this to false once it's on the live server so that it doesn't show any errors and then to utilize this we do this in the Define and debug so what we'll do is we'll go to the index page right here after session start oh they need to goes there right now before this after we include all of that we can write here and say if debug like this then hmm how do we do this 50 bug we'll say ionized set the variable name is display errors and we're in debug mode so this will be set to one and then else you to be set to zero alrighty then so here instead of putting it like this you can make this a one liner it's very possible you can do this you just put a question mark there and then run this here and then put a full column if things are not the way they should put zero like that so that way it's a single line thing yep and let's remove that this is just an if statement saying if this is true then run this if this is false run that okay so let's see if we don't get any errors when we try this okay we don't so currently the debug mode is true so let's try and see if we can get some errors so here I'm just going to do this put a 3 there so I know I'll get an error okay so there is an error now let's put in our config let's put this to both so that we are not in debug mode anymore and if I refresh you see all the errors are gone so that's one way to make sure a simple way to just say true or false to make sure errors can show or not all right so everything is cool what have I forgotten I'm thinking I've forgotten something oh yeah I forgot how this one right there okay so let's fix this so this part here is the part that I want to send um if I go to my home controller here these right there these variables I want to receive variables in here so for example here if I say let me remove all of this let me just say show a I want to see what's inside a right now so if I refresh there's nothing there as you can see on the home page let me just remove where's the Home View let me remove this image and of course let me delete it from the images folder this should be a bear framework as as simple as possible let's delete those guys let me remove that as well right even so if I refresh that's all I get home view page I don't get any of this but if I come back to up actually if you look at this you see this home this slash d slash some characters and then slash something else here so that's what I want to talk about I only managed to grab the home which tells me what the page is but I don't know what these other params are because those may be IDs to a specific record that I want to access so I need access to those in the controller that I have loaded because the first one is just for telling me which controller to load the second one is for which method to load okay now we haven't gotten to that yet and then the others should be put in here so what I want to do now is get back to app and here I will put this URL there so copy this and put it in here so now what this would do is you see I have home there which is nice as the first item here so if you look in the home controller I have B and C so if I'm going to do this show you B and show you C you see that they have shared those values equally this home there's D and there's that but also if I say d actually uh this is fine so I'll leave this as it is let's go back to up here I want to show what's inside the URL one more time because there's a slight issue I forgot so if we come back and refresh this is what we have now look number three is empty that's because I have this slash at the end here So to avoid that I should trim this URL uh before trying to use it so I'll do trim like that and then inside the trim function I can tell it what exactly to trim so I wanted to remove all the slashes that are trailing or leading that way we have a clean plate we can get rid of number three and there it is because it doesn't have a value at the end anyway so at least now we have these nice values here now what I want is I want to use number one home is already used for finding the page I want to use one to find the function So currently the function will that will always run is the index function now what I want to do is show you that we can have another function here for example the edit function so here I just want us to Echo that we are in the edit function from the edit function okay like that all right and this one will be from the index function so let's come back here and refresh let's see what we have we are in the index function right now but if I change D to let's say edit we are still in the index function so that doesn't change so what we need to do is tell it to use that so here I will do exactly what I have done here but at this point I want to check if a method within you see here we've instantiated the controller now I want to check if there's a specific method inside this controller so luckily there is a method exists so what is the object the object is this one which is the class and then what method am I looking for I'm looking for whatever method has been given actually for now I'm looking for item number two on the URL here so now item number two may not always exist so what I will do is first of all we have to check if it exists hmm so I will do this if method exists so do that I'll put one here now it's possible this may not exist so let's put that and just to say if it doesn't exist put the next best thing oh wait a minute actually let's just do this this is not going to work very well so say if not empty okay so if there's something in there then let's check if that's something is a valid method so if method exists that's something okay so if that method really exists then we are good to go then we just change the method to this one oops so we say this method this one that's at the top here we change it from index to whatever this is because it does exist so let's change it to that and that's great that's all we need to do and then here we can use that so great this right here deals with the method select method the other one selects a controller already then so let's see this now so you see this is the edit function now that's running because we have edit there now if I put something that does not exist you see it reverts to the index function if I go back it goes to the edit so now I can say product slash edit and then here I can put a 1 to tell it which we're going to edit so here I'm using the edit function home page in the edit function with record number one that's what we are doing here now this record number one this home and edit I've already used them so I don't need them all I want is to grab what remains because this could be one slash something slash something I need to grab the rest of them so what I need to do is every time I use one I should delete the others so here let's say I set this controller to this I should unset this guy so say unset that guy okay because I've used it from the URL same thing here if I use this let me unset it from oops what have I done let me remove it from the URL thingy there and then just send whatever remains in there so let's see now if I refresh nothing seems to have changed but so I copy this and say show URL you will see that only these are remaining you see the [Music] the one this one and that one but home and edits are missing from here which is nice but if I say edit was not found they say I put some text you see that now it's added to this because it was never unset so it's part of the variables instead of being a method so very effective and now let me just see which ones of these items I will receive in here so let me duplicate this let's see what I receive in the a b and c so a b and c so from the index function I receive that that and that which is nice as you can see now if I take this to the edit then I can receive some different variables so let me put them here so that we see what we receive so I receive everything else apart from the word edit there so everything is working as intended so I can use these guys as IDs and all that so let me remove that let me remove this and let me remove this so this is all it should have now if you don't like extending you'd rather use controller to do this you can say use controller you can change and remove this you can change controller into a trait as well I need to work exactly the same like this come back home and then you just need to always remember to put use controller just at the beginning here and then you can put all your functions down there so in this case exactly the same thing no errors whatsoever so that way you will use traits instead of extending these guys so it's up to you which method you want to use really doesn't matter okay so with this in mind I think being consistent is nice so that you use one method that's up to you at this point we have completed our MVC system it works we just need to test it and see how we can make an actual website from all of this right so you can go ahead and add more functionality here by putting more functions as many functions as you need but the rest of it is done so let's do a simple test here and see what we can come up with to try and create a simple website all right so let me explain uh one more thing the use that we see sometimes you're going to see this use being put here like so right this will work just fine as well the use statement non-compound name controller has no effect yeah yeah I know it has no effect okay so this is for namespaces this is for trades so it may look similar sometimes but they're very different if you put the use here you're telling um you're telling PHP that it should grab all classes that that are in the controller namespace and load them into the current namespace here so that's a topic for namespaces when it's up here but when the use is down here then it's a trait like a class that you are importing in here so that's the difference there let me try and clean this up a little bit I want to copy this go to home controller and let me just type um class like that okay so now let me close all files we have a complete MVC system here that's working so I'll close all that very nice so if I now open the containing folder so what you need to do as a programmer is to keep this folder safe so every time you want to run a new project just copy this folder and rename it and then you can start your new project that way you don't start your project from scratch so let's see an example of a simple project where we insert some new users and [Music] um maybe a sign up and login let's see how that can work out so to begin with let's copy like I have said this folder I'm going to just copy the folder and paste it right here so this MVC copy let me rename it to test MVC that's all I'm doing here rename it so now we have a different project so I'm just going to remove folder from project and then grab the next best thing so text test MVC I'll grab that and bring it in here so all I've done is made a copy of it so that whatever I add Now does not affect the original so I just want to see how quickly I can create uh pages from this so now if I go this is inside test MV let's go to our browser here and then change from localhost to test MVC slash public okay so this is what you get now you can design a nice home page for yourself because the home page is required always there so you can design something nice uh to land on now um I can then go to what's this let me make sure that if I go to controllers the home controller doesn't have anything in wonky okay that's fine so what I want now is to create some kind of a home page right so to create a home page we can grab any template or anything like that but let's let's try we have we have bootstrap if you go to the bootstrap website which is getbootstrap.com you can go to examples here and then you can download examples now the examples are these Pages here created with bootstrap now I'm just using this because I want us all to be on the same page because you can easily get these but you can use any template so there's a sign in page here and maybe what else can we use I want something that looks like a home page there is a Heroes here or headers etc etc or maybe cover like this one right here so cover page so I already did my own download here oh this uh bootstrap example so I'm just going to grab bootstrap examples here copy it through my folder paste it here okay so there it is now if I extract so I don't need this zip folder anymore so there it is bootstrap examples there it is so what I want is to grab this cover page right so this one should be in the cover folder where is that right there so it just cover CSS and there's HTML so what I would do is I'll grab these guys these two copy okay then come back to test MVC I'll go to um this is for the home page so the home controller already exists so we'll use we'll use that and the home page view already exists so we'll use that but what doesn't exist is the CSS so that has to be in the public folder so we're going to assets and CSS and paste it there then I will grab the index.html page come back to up views and paste it there okay so with that we are here so if I go to public um assets CSS you see there is the kava CSS if I go to views Index this is the index page so I can copy everything from here go to my home page select everything and paste and Save so if I come back now to my actual page which is this one if I refresh it this is what I get but it doesn't really look like what it should because some stuff are missing if I view the page Source you see that the stuff that is missing like the CSS file if I click on this file node found you see that it gives you the 04 file not found so let's go in through this here and see what files should be there that are not so like this one right here this should be a bootstrap that mean.css file it should be inside assets disk but this is different we will put it inside assets but inside CSS and then bootstrap but then here like I said we must add the root so okay so once we do that that should be good everything now must be we don't need this generator you can edit whatever you want here change everything I'm just concerned with the uh the CSS there's also this cover.css which we have already put inside assets slash CSS slash cover CSS so if I refresh I don't see a difference that's because the original bootstrap file isn't there so what I want to do is go back to the bootstrap folder because this should contain bootstrap in this assets folder so here there is inside distribution there's CSS and then there's the JS so I can copy these guys from here so I'll just grab actually I can just grab the whole folders here and go to test MVC assets and just replace because it won't delete the files already existing so you see cover CSS is in there and we have JavaScript files now so if I now refresh it should work out but it doesn't seem to so let's look at the source and see what's wrong with the URL so there's HTTP localhost MVC but okay so it's in the wrong it's that's another thing you have to keep in mind you have to go to the config and then put the correct path here okay so instead of MVC public this one is a new four days test MVC public okay that's our new folder so if I now refresh this is what you see so everything seems to be working just fine you have these Pages home looks nice and it's working as intended so this is how quick you can get set up and begin doing things so let's try and put a login page because the examples also have a sign in page so let's come back to test MVC inside the bootstrap let's look for signing so this is the sign in page it has this and that so let's grab those two babies back to our test MVC I'm going to public Assets in the CSS I want to paste the sign in.css then cut this guy let's go back to up views there's another index here we copied from but let's replace it since we don't really need that file just there to for us to copy from so I'll copy everything from here now here I want to create a login view so I'll paste here save this as login.view.php okay then we can remove the HTML file we don't need it everything has been copied over and then right here we need to put oops we need to put our root like that make sure it's a correct path assets there's no dist there's just CSS and then bootstrap.in that's fine let's remove that guy we don't need him we don't need these names of the authors uh then they sign into CSS so similar to this it's in the same CSS folder so we just put that over here cool so now if I go to my home view page this is that part where there are links is home but this one let's put login and what can we do here login page there and then all links even the home page here should have root like that so root is home and then here we have root slash login so if I go back now let's refresh this login there if I click on it cannot extend the trade oh sorry about that you know this is one thing I forgot [Music] um because we say the extend controller we change this to something else so I forgot to change this to this has to say use controller like that because we changed it to iterate sorry about that forgot so you must change that in the original folder as well so this is what we get page node found so you can design your page node found as well to be nicer that this find a nice page not found and put it there as well but if I go back it's there so the reason the login page isn't found is because we don't have the login controller even though we have a login view so all you have to do is copy the home controller create a new file paste and then change your references to home to login like that and then save it as capital l login.php okay good so now that we have a login controller we can click on login and it will take us to this particular page okay it's just that the logo is missing because we don't have bootstrap icons this is a bootstrap icon but you can put your own image there and it will work just remember to put root inside the path but otherwise this is cool right so we can have login and sign up and then on our home page let me come back to the login View and right here I want after it says sign in I want to be able to go to the home page so I'll put an a tag and just say root as the path like that okay and then put home there that's all that way it's easy to go back home and go back to the login page we can also put a sign up page here so let me just copy everything from the login view copy new paste save inside views make sure you're saving as a PHP file for all files let's say sign up Dot View dot PHP and same thing with the login copy everything uh paste change your references from login to sign up same thing here sign up and save that as signup.php with a copy tool so this is all you do when you want to create a new page just put a controller and make sure you put a view that you're referencing here in the controller it's that simple that's how you make new pages so if I go to the login page I can also put a link to the sign up page you can say sign up and then here all I need to do is Slash sign up like that and then copy this go to the sign up page and put a reference to the login page as well and change this to log in and put that to login so what I'm doing now is if I refresh you see this home they sign up this is a sign up page now it leads to the login so let me click on sign up so let's quickly add a sign up situation here so please sign up you can remove the logo so this email address and password so I just want a simple thing and then remember me here I'll name this one remember no actually I'll name this one terms or accept the terms value can just be one it doesn't matter accept terms and sign in what else let's come back refresh oh what have I done let me just say create account like this okay create all right that's much better so this email address and password so I can create an account and accept terms here like that okay so now let's see how we can use this to Advantage okay let me go back to our login page please sign in email password okay cool then let me add names here as well so the inputs here and there should have names this one is email the other one is password now I have other tutorials that are in depth on this this is just to show you how an MVC system can help you create something very quickly so with this here uh uh if I now do some creating once I click make sure here the method is posted same thing on the login here otherwise it won't work so we put a post method there once I send this data it will go to the sign up controller so right here on the sign up controller I can simply do show post like that just so I can see what's inside the post if I refresh you see it's right there but let's try email at email.com and password to create an account so I'll say create and you'll see the information there right so now all we need to do right here is just create a new user um user is equal to new user class okay it will try and find it from the models section and then from there we just say user insert and then we insert everything in the post variable now if you want to be able to validate you can use this user thing as well you just say if user and then you say validate and then you put this post information right there to validate the information before you save it so if this return is true then you insert okay and then after you insert you can redirect the user so we can create a function which is also useful redirect and then let's redirect to the home page like so now this is notified function so we're going to have to create it here now every function you add here go and add it to your original MVC to make it grow bigger over time so function redirect whatever functions new functions you think you would always need just put them in here and put them in the original as well so let's see path this one will redirect to the root slash we put a slash because root doesn't have a slash and then we put the path like this so to redirect we use the header like that and then say location two column and then do that concatenate the path like so then make sure you put die so that the script does not continue after they redirect so copy this function to the original MVC as well put it there after you are done with this because this will tell you what new things you need so once we inset we do that now the user thing doesn't have a validate function so like I said you can go in here and actually add some stuff so usually the functions you need to add here are all public because they'll be used outside it depends so we're adding a function validate here which will bring in some post data now I don't want to use posts like this I'll just rename it to data so it doesn't get from the cost variable directly then all you can do here is this you can you can go to the original model and give one more of this item code errors this should be equal to an empty array like that so that it's always it always exists so let me do that just to be neat okay so we have errors which is empty nice so once you instantiate here always set this errors to an empty array just to make sure just in case last time it wasn't you make it an empty array and then at the end you just check if empty this errors if there's nothing in that error array let's return true otherwise we return false so we return Force to say validation failed this one means validation is true because we didn't get any errors and then in here so if you want you can keep this function in the original MVC as well because I'm sure you will always need to validate information then all you have to do here is check whatever data you want for example if you say if empty data email so if the email wasn't sent you just say this errors and then you give it a key there I say this errors email with the same then you put your error message email is required oh then you can put an else statement if you want put another if if data email is a way to validate emails using PHP you can say um filter VAR you put the variable there you tell it how you're filtering it you say filter validate you can check this on php.net to see how this works but this is how you validate an email so we'll put an exclamation mark to negate that so that if it doesn't qualify then you say email is not valid okay and then you can do the same for a password the password is required just go down and add some more and say change this to password uh password is required just like that and then the errors are saved in there now since the errors are saved in a variable in the user's class it means if I go to my sign up let me close all this right here user validate if things do not work out then what I can do is I can say errors is equal to user hairs like that so whether there are errors or not user errors always exist because it's part of this class and then you can send it to this so this will either contain the errors that we encountered here or it will be empty if there are no errors but in that case it won't matter because we'll be redirected anyway so if we are not redirected it means this didn't work out so we will have errors down here that's all you need to do now is send your data here and that's one thing I actually forgot sorry about that we need to have a space here to actually send data to the view because actually right now the view cannot receive data from the controller which is a tragedy so what I need to accept that is to go to the controller part so here there is view the name of the view but we can put an extra thing here for data and set it to an empty array just in case [Music] it doesn't exist so we'll say if not empty uh data if there is some data what we'll do is we extract that data like so that's what we need to do in order to make sure that this data is sent down here because this is included from here so anything we extract here will be available to the file down there so that's another thing to keep in mind so once we insert now if we do have errors I can go to my sign up view right here we can do a check to see if the variable errors exist now in order for us to be able to send it in the first place this variable like this will not be available here because we are only sending this one so what I'll do is I'll say data like this data errors so that once we send data it will contain a item called errors in there like so okay cool now in the view of the sign up we can check if errors is not empty and Echo out the errors so here I can put a div now since we are using bootstrap we can put some bootstrap classes like alert alert Danger like that okay and then we can put some PHP tags where we Loop through so since it's MVC always be mindful whatever um logic you want to do should be done in the controller in the view you should only do loops and Echo Loops Echo that's it anything other than that should be done in the controller that way you limit how much PHP code is in the view okay so loops only loops and Echo so in here since we are putting a loop this is fine so say errors as error foreign let's remove all of that so each era we're just going to Echo it out and and in fact there's no need to even do this I can just implode this so let me grab this right there and just say implode the glue will be a brick tack that's all and then the pieces errors like that so I'm just going to Echo this this is Simco now instead of doing all this Echo I just do that and that keeps it very nice and neat then here I can say PHP and say if not empty errors put a full column close that and then put an end if right there like so and let's do that very nice so that only when it's not empty do we get this alert so let's see that so if I click create cannot access property errors because it is protected sorry about that so if we go to model here this should not be protected it should be public okay that's it so refresh resend emails required password is required Okay so if I click password is required but this one is gone so you can put functions in there to give you the old value so you don't have to retype it again but for now this is all we need so we just want email and password in our thing and also keep in mind that the config you should put your database file under the database information to make sure you're accessing the right database but here I want to empty my table so go and go to operations and say truncate this is the users table in my DB if I go to browse this is what I see and this doesn't qualify with what I want so I'm just going to go to the structure and change it name I will change this one to email because that's what I have to save that age um I'll change that to password so change that to password and password variable character let's put 255. let me save then the date will leave it as it is so that's up to us so now also remember that once you change the columns in there they should reflect the same thing here in your uh model so email and then password those are the editable ones otherwise we wouldn't be able to save anything all right so there we go so with that in mind if I come back if I click browse it's empty right then if I put email and put password there maybe we should accept the terms so here I'm just going to go to [Music] this part as well duplicate and put terms here so we'll say please accept the terms and conditions yep if that's empty then we don't go so please accept the terms and conditions so accept email s word and then let's create and as you can see now things have worked out if I go to browse you see that we have a new record there very very nice so now if I go to my login page I should be able to log in now how do I know I'm not logged in here actually once we sign up we should be redirected to the login page I guess then if I copy all of this for my sign up page for my login page I sign in I can paste that in here now I don't need to validate this I just need to read from the database right and I don't need to insert so I need a wire Clause I just need the first um that's the function we created so this redirects to the home page it should work if things work out also we need only do this when something was and that's what I forgot here this is crazy uh I should put a if server request method if server request method is equal to cost then something was posted this is the only time this is the only time we need this code to run like so when something is posted so copy this back to the login controller as well okay cool so when something was posted we tried to find a row right and then redirect to home if everything goes well but once we read from there what I want to use here is not just the post but I want to read uh the email from there right let's apply actually this is not what I need I need to put an array here so let me just do this and say array email is equal to so we're just looking for an a record with whatever email is applied if we find the record then we check the password so we say if row and then check to see uh if the password in the row is equal to the password in the post then we can authenticate and redirect so authentication is up to you in my case we can just use the session variable like this and say session user is equal to rho so whatever row we got from here let's save it in the users in the session because the session variable is always active as long as the session is active so we can just save that entire row in there if you want and redirect to the home page as simple as that otherwise let's do the error thingy so I'm just going to copy what I put here the error message to the login page as well where did I put this right after the form like that okay cool so if we have errors they're gonna show up there and Back Again to the login page I need to make sure these guys have names so name email this one should be named password remember me could be another issue which we can't do here now cool so if I go to the login let me try and log in and okay so I'm not getting anything there no errors that's because I didn't set it up that way so here if the fire is not found if we are posted and we do get to this point then we will have some error problems so what I can do is add an error to this errors thing it uses errors email is equal to wrong email or password so we're simply adding an error to the user errors part and then setting it to here so that we can send it there so data is required so wrong email or password add it there that's good let's see if that'll work so it says wrong email or password let's try and add the real thing and then put this word there let's see if that'll work and it worked now how do we know it worked we should add somewhere where it says hi and show you who you are so if we go to the home view here and we can Echo out something that exists so I can go to the home part here right here I can say something like username is equal true and then say empty [Music] session user now instead of me having to check if the session user is active because I may want to change which of these I store the data in so you can create a function that just reads from this session variable called user right and then run that function instead of having to read directly there but this is fine too so if this is empty we're going to set this username to uh simply user and then if it's not we will set it to user email whatever the email is of that current user and then since we want to send it here we want to use a variable called Data and so we will do data here username like that so that it's sent to the view site as username Okay so what this enables us to do is to avoid checking putting an if statement to check because username will always be set either to be set to the word user or the email okay or you can set it to unknown so now if I go to my home view page I can simply come to where does it say your cover page cover your page your page right there so right before this one I can just put maybe H4 tag here and just go out username all right like that so if I now do this there you go that's my email so I can just say hi to make that hi hi so let's put a log out page as well so we can see that we are able to log out so log out just like this is we'll copy that put it here and put log out like so let's refresh all right so we should have a logout page easy peasy now the logout page doesn't have a view so all we need is to copy maybe from the home controller and right click new file paste change that to logout [Music] log out here as well let's remove this view because we don't need a view at all this is logout.php let's redirect to the home page now all I need to do is check if this session is saved so say if not empty then unset it simple as that and then redirect that's all so log out and now you can see I've logged out I'm just a user now if I log in oh and defined variable data on line 33. let's check that out yeah that's one thing I forgot um the login there's data here that we are only creating here we don't have any other value that so I think it would be better to just leave this so at least we have something that this is an not undefined right but the problem is user is being created here as well so maybe what you could do let me undo this maybe you can just create an empty version of it say data is equal to empty array that way it's always there regardless what happens later so same thing here we will need to put it there like so okay so refresh login page let's try and log in email password sign in now I'm signed in look at that if I go and log out try to log in maybe let's try to sign up so let me try Mary at email.com password I need to accept the terms create now I can log in as Mary password and login now you see Mary very nice if I try to log in as somebody that doesn't exist it doesn't work yeah so back to the home page I'm still logged in as Mary so this is how a MVC system can help you out to create a a website can speed things up so I'm going to be using this MVC system in a lot of my projects because it's easy to get up and running when we already have an MVC system so uh one thing to note is that we edited our so let me grab the we edited some things so let's update our MVC our original MVC so I'm going to grab the folder bring it in here as well that way we can compare and contrast so for example the 404 I changed it to have that so let me copy that go to MVC app controller 404 paste okay let me close all files just to avoid confusion and what else did I change I changed let's look at the functions there's a redirect function now so copy that let me go to the original MVC actions let's add a redirect function there cool what else did I miss in the model I added errors now so let me copy everything from the models thingy into the models for the main model here select or paste save in the user model as well here I added [Music] um this is a much better one because it has validation as well so let me copy this user model into the other one paste very nice I think that's all we really changed that needs changing the rest we don't because we don't need more pages in here more views or anything like that so with all this to conclude if you want your MVC to have uh a basic home page like this one and not just to see um because if I go back to just MVC here this is the home page we have for our original MVC if you want you can use bootstrap and use this as the basic landing page or the home page that you don't start from scratch but that's up to you if this is useful to you or not or you can just leave it this one so it's all a matter of knowing what you always repeatedly use and then put that in advance in your MVC system so at this point our MVC is done and it works as intended so we can close it you can even zip it as a zip file if you want the zip folder so that you extract it whenever you want to create a new website now I'm going to be using this same MVC for most of my projects from now on so that we don't always have to start from scratch because a large part of my tutorials are wasting time on creating an MVC system so I will be referencing this video instead alright so hopefully you have learned something new and I will see you in another tutorial
Info
Channel: Quick Programming
Views: 70,501
Rating: undefined out of 5
Keywords: Web Design Programming Tutorials, web development, quick programming, PHP, MYSQL, HTML, CSS, tutorials, Learn, learn to code, course, coding, json, javascript, svg, website, programming, php tutorial, php login tutorial, how to create a login system in php
Id: q0JhJBYi4sw
Channel Id: undefined
Length: 226min 32sec (13592 seconds)
Published: Fri Nov 04 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.