PHP: Create Your Own MVC (Part 1)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey what's up youtubers today we're going to make our own Model View controller and I'm in NetBeans and I've created a new project called MVC inside source files I have my index.php file and i'll just type one in there and in my web browser it's under this page and we have one ok nothing here yet what we're going to do first of all is make it so that we can type a name like let's say dog and that will load a controller called a dog a controller called dog and if we were to say slash test it would load the controller dog with the method test and this should make sense shortly and to do so we first have to make an HT access file to redirect our calls so I'll do dot HT access and the first thing we have to do is turn our rewrite engine on so rewrite engine on sorry that's one word and you need mod rewrite on in your PHP ini file obviously and we're going to do a rewrite rule of anything and we'll send it to index.php so what's going to happen is anytime they type in a URL it's really going to go to index dot PHP and we're going to pass a query string called URL and this would be dog okay for the example I just gave a minute ago so we'll do URL equals dollar one and we'll do query string append and symbolic links you might also want to add this I'll just do a real quick rewrite condition what's that request file name not a directory not a file not only ok what this basically does is this can't have a why it's not going to redirect us with this argument if it's a actual physical directory or an actual physical file so anyways let's go ahead and make our folder so let's start with controllers models and views and I'm going to make one more folder and call it libs short for library ok I'll save this and let's see what happens if we go to slash dog good it's at the index page all the time that's what we want so what we want to do is get the string get URL so URL equals get URL let's just echo URL ok refresh and we have that that's exactly what we want so what we're going to do is make a new controller I'll make a empty PHP file in the controller's I'm just going to call this index for our main page and I'm going to type CLS which is short for class press the tab key and it does it all for us and I'll call this index just press ENTER to get through this and I'll say echo we are in index okay so here's our index controller I can go ahead and close that and what we need to do is require the controller if we're at the URL so you see I'm refreshing let's go to index and it just says index but what we have to do is require controllers /url dot PHP make sure we have no errors okay refresh no errors and we can instantiate a new controller so I'll say controller equals new what's that called index or we want to call it the name of the call so let's see if that works and we are in the index cool real quick I'm going to make another controller and I'm going to call this help oops PHP and C LS once again in the tab key and I'll just call this help enter enter and echo we are inside help okay so we have that created I will refresh and let's go to slash help and we're inside help pretty good so far now if we want to call a method in here such as other we need to handle this slash here so while we get our URL you can see it's help slash other the first argument is always going to be the controller name the second argument will always be a method in the controller so to do so I'm going to explode the URL so explode URL shoot I don't remember the order OC explode the delimiter and the string okay I want to explode by the slash of URL so we're just going to redo this I'll just pop this in here okay and let's do a print are of URL so refresh and we have help and other now the first argument will always be the controller so I can always require URL zero new URL zero and let's see refresh and we're inside help and right now this isn't doing anything even though we have more arguments so what we're going to do is check if those exists so if is set URL one controller and then we're going to call URL one function that looks kind of crazy what this really translate into is controller function that's what this above is translating into so let me open up help real quick and create a new function oops public function other echo we are inside other and save refresh help online nine what did I do we are in side other I forgot my things refresh and here we are inside the other now method so and if we wanted to do more slashes we could kind of repeat the same pattern where we would say we first check if there's an argument so what would what would what this would do is here's other and if we had like let's say a number here ten this would actually pass the value ten inside other this is up to you if you want to do that and I'm first going to check if that value is set for number two because we have to check that to see if we have to pass a value instead of calling the function first so if Sat URL to go to the URL to okay and in here let's just say R equals false so optional arguments and echo optional will be hard so we're just going to see if we can get an argument in here refresh and here we go optional 10 we are inside other optional let me do some line break so this is easier to read a slash br br okay refresh and we are inside other and it's calling optional 10 and it looks like it's calling it twice because I didn't do this wrong else will pop this little thing in here okay refresh and we get that cool now what do you what do you do if there's one that doesn't exist if we go to help undefined method help well that's kind of weird look at that backslash we're going to have to clean off this backslash it looks like help-help so we're going to check if it's four things empty for a quick and easy hack I'm just going to do a trim of the slash so I'll do I'm gonna actually put this back how I had it and I'm going to do an art rim so you are equals are trim which means right trim of URL and we're going to trim the slash just a quick and dirty way to do that let's see if that fixes it help and without the slash help that'll be good if they had a bunch of slashes I guess it still works shouldn't be a problem okay now we need to probably encapsulate this into a bootstrap object to make it nice so I'll say new PHP file inside the libs I'll just call this bootstrap and CLS tab bootstrap enter enter okay and in the index file I'm going to grab all this junk cut it pop it in bootstrap and I'm going to source format this will fix all my tabs up okay so get the URL trim the URL explode it load the controller what we're going to want to do is check if the file exists so file equals this and we're going to check if file exists a file then will require file okay else throw new exception and we're going to say the file file does not exist however you handle errors is going to be up to you so refresh and if we go in to test that shouldn't sorry I didn't even instantiate the bootstrap that's embarrassing so up in the index file we need a let's just call this app equals new bootstrap and when the bootstrap is constructed it checks all this junk so refresh class bootstrap not found once again I forgot to require this so require lid slash bootstrap that PHP try that once more okay and test does not exist help exists excellent help slash other looks good and we have an index one what we can do also is if there is not a controller we could create an error controller so I'd call this error and we'll just kind of do the same thing CLS air enter enter and echo this is an error okay and this will handle all our errors so back in our bootstrap instead of throwing an exception we'll just do a require controllers slash air dot PHP and we'll say controller equals new air and I think that'll work and next to two to not found what would that say this is an error okay we're in the air controller on line 21 we're getting a not found oh let's just do a return false so it stops doing so there we go so we'll have an error controller there I can get I can hide this for debugging okay safe and MVC so this is nothing here if we go to index there it is if we go to help there it is and other there it is and that gives you a basic idea just a quick and dirty way to do some controllers I would maybe break this apart and you shouldn't have your controller name in here this should be configured in a metadata so that if you ever wanted to change your directories you change it from one file like a config dot ini file or something so that's good for the controllers for now what we want to do is have a view as well so let's see index bootstrap to get a view there's a couple ways we could go about it I want my view accessible from the controller so what I'm going to do is actually make a new PHP file call this controller and this is going to be the base controller the every controller is a child of oops PHP CLS controller enter enter and what I'm going to do is just echo main controller okay and in our other controllers err this has to extend controller extends okay help has to extends controller and what else do we have error index help there it is and so we have to construct the parent so I'll do parent construct what this does is it will construct the parent controller let me give you an example real quick let me put these in all of those parent construct and towards the air parent construct I'll close I'm going to close a couple files to try to simplify some of this I'm holding ctrl and shift and pressing down to duplicate a line by the way if you're wondering how I'm doing that so we're going to include the controller and let's say refresh so back to our index and our main controller okay here's our main controller if i refresh we get our main controller properties and something in here if we don't have this refresh wait did that work that doesn't make any sense anyways I'll just leave this like this for now so we don't get carried away the main controller is going to have the view so we'll say this view equals new view object and the view will be a object here new PHP file called view enter and before we get all confused we got to make sure we require this oops you I'll probably put a note here use an autoloader you'll probably want to use like SPL autoload or something so back to this same old thing CLS view enter enter echo this is the view okay so here's our controller let me just make sure everything works i refresh we have a view because we instantiate it inside the base controller and it might be hard to see what is really happening here I have some videos on how MVC kind of works I hope this isn't too confusing so here's the view what we're going to do is do a render a function so public function render and we're going to do name and we are going to require views slash name dot php' okay and we got the views working now we need to make a view so I'm going to put these in subfolders so let's say my index page will have an index folder my error page will have an error folder so on so I'll make a new PHP file and call this index dot PHP and HTML head body blah blah blah this could be all that stuff right you could also include this separately like if you wanted to do a view new file call it header and you could do HTML header body and we could just say PHP require of you slash header dot PHP oops this is the error so let's go to error okay we're inside the error and what we want to do is go here and we're going to do this view render err oats air slash index refresh header this is the error cool and if we wanted to assign some value to the view you could say this view of message equals this page doesn't exist okay so index.php in the error area and that could say PHP echo this because we are in the view object message okay so i'll refresh and this page doesn't exist and everything's kind of delegated through each other and mixed and matched in your controller actually never mind in models I'm going to create a new model for I guess help and I'm going to call this help underscore model the reason I'm doing this is because if you just call it help it's going to conflict with the help class whoops what'd I just do help model and then PHP so CLS tab help model enter enter okay and echo help model and this is where your business logic would lie let me close all these because this is getting a bit busy so what I could say is when we get to other I could say I'm require of model slash help model dot PHP new help model equals new help model is just a dirty way to do it so that if you need you know you can sit around and try to figure out a pretty way to do it there's just not enough time in this tutorial I don't want it to get too long so if we have a model here's help model I'm going to make a base model also so I could shrink some of these down our libs folder we're going to make the base model so model in PHP so CLS model enter enter and a model often includes a database so you might do something like this database equals new database but we don't have that set up here and I can go ahead and close that in my index file I got to make sure to include the model okay and once we require this the help model these two extends model let's refreshing cross our fingers and hope that works help we're inside help and other help model cool we got it to work I'm going to make this code downloadable at my site I'll put a link in the description so you can go through it and try to make it a lot better for yourself and this is just a very basic structure for a MBC example thanks for watching guys if this helped give me a thumbs up because I'm burnt out from trying to talk about this Thanks
Info
Channel: JREAM
Views: 603,188
Rating: 4.9053912 out of 5
Keywords: php mvc, beginner php, mvc tutorial, oop, tutorial, custom mvc, learn to code, mvc, jream, php tutorials, ood, programming, programming tutorials, create your own mvc, coding, php tutorial, php ood
Id: Aw28-krO7ZM
Channel Id: undefined
Length: 22min 46sec (1366 seconds)
Published: Wed May 18 2011
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.