 Hey everybody, this is Brian and welcome to the 7th Ye2 Tutorial. I actually need to take a pause real quick and explain a few things. Not about Ye, so if you don't care just fast forward 10 seconds. I had installed the Windows 10 and the newest version of Ubuntu, Ubuntu 15. And in doing so I wiped out my hard drive. I said, oh I don't need any of this garbage. And well, yeah, I forgot to back up the website. So I have since recreated it, so if there's some inconsistencies between the previous tutorials and this set of tutorials, that's wise because I had to go back through and recreate all that code because, you know, like an idiot, I just didn't back anything up. So anyways, now onto the tutorial. We are going to cover the controller class in all its glory. If you remember, the paradigm here is the model view controller. And the model is a representation of the data. The controller controls the flow of data. And the view, you know, represents what is seen on the screen. Well, we're going to deep dive a little bit into the controller. Get a feel for what it is and why it is. I know we've talked a lot of theory. So, if you go out to the Ye framework 2.0 API documentation, you can just Google Ye2 controller and it comes up. You'll see there's some public properties here. And you'll need to notice like the layout, the ID, the module, modules, the route. That's important. We'll go over that real quick. The unique ID and the view and the view path, et cetera, et cetera. A lot of that is set for you. And there's, I mean, there's just tons and tons of stuff like all the public methods you can see. So if you're ever like, man, how do I get the current view? How do I get this? How do I get the route? You know, pretty much just covered in the API. So we're going to flip into the code here. And whoops, I've got some code open. Yeah, that was me rewriting all this garbage. So if you remember correctly, we had our little pet store here. So we're going to go into controllers. And we've got two controllers now because in the previous tutorial, we actually used a key to generate the crud, the create, read, update, delete code. That's in the pets controller and the site controller, which was whoops, the site controller was automatically generated for us when we created the website. So we're just going to kind of pick these two apart real quick, see what the difference is. All right. So when we go to our website, let's actually do that now, pet store, web, tada. And let's just click on a button. You notice how the query string here changes. Well, that's the route. You can tell it's the route because it's r equals. And we've talked about the route a little bit before, how this %2f is really just HTML encoding for a slash. So your controller is the site and the view is about. That's why the slash is there, tell the difference between the two. So controller slash view. So we look in the site controller about and we can see exactly what's going on here. Now, remember these actions here? There's an action index, action log in, log out, contact about. Each action represents a route. So when you see the word action, think route. So we're in the about action. So here we're just literally rendering the about page. And we can go down into the views. And you notice how there is a folder for each route, or I should say each controller. And we're in the site controller. So we want the site route. And then there is the about page. And that's how that's being rendered. So that's kind of the natural flow of the controller. Literally what the controller does is flows the information. And we're going to jump into the pets controller. And don't worry, we're not really missing anything here. It's the same thing. It extends controller. But it's got a couple extra little items in here. For example, like the verbs. Well, what's a verb? And you'll see it uses the verb filter here. A verb determines what's going to happen. I dare say what sort of action you're going to take. Like here's an action. When you go to delete, it's going to use a post verb. Meaning instead of HTTP get, it's going to do an HTTP post. Man, it's been one of those days. But the same thing. We've got action index, view, create, update. So those are basically routes that we can go to. So let's just go out to our... And we don't have a link in here. But we can just go pets. And there it is. All it's glory. Now by default it's going to do index. So this is the same thing as doing slash index. It's the exact same thing. It's just going to do it by default. Now if you hit create, notice how it goes to the slash create. Or it goes to the create view. So let's actually go out to create. We'll look at this real quick. You can see here we're saying a model. Remember a model is a representation of the data. Equal new pets. So what's happening there is we're going out to the models class. Grabbing pets, creating a new instance of this. Remember the model is the representation of the data, not actually the data. So what this active record does, because the pets class extends or inherits the active record, this actually ties into the database. So it does all that data controlling seamlessly in the background so you don't even have to mess with it. So we're making a new record essentially. And then we're saying if the model, that pets record, can load the post, which we've talked about that before. So it's just going to take that whole post and shove it right into the model automatically. And you can save it, then redirect a view. Otherwise we're actually just creating a new record. So if we're not loading the data, then we're just going to render the page. So this is what happens initially when we go here. Seems a little confusing, I know, and that's why I wanted to go over it. So once again, we make an instance of the pets model. And this is essentially like creating a new database record. If you were just to say, whoopsie, model, save, that's the same thing as creating a new record. But instead, what we're going to do is we're going to actually load the post. And this will have the primary key and all the data in there, right in the post. It's all encoded so that it goes directly into the model. The load feature does. Seems kind of counterintuitive that you'd flip it around, but you could really do it the other way. This is just how ye generates the code. And you kind of got to get used to that because when you first walk into it, it's very confusing. And we have this and operation model save. So if we're loading and we can save, then redirect. Else we're just going to display this view. I personally don't like the way this works because if you have a problem saving, you're like, hey, why didn't that save? Update, same thing. I mean, you can see it follows a very similar pattern. Delete, this is different. We're doing this, meaning this controller, find model, which is this function down here. And what find model does is it says, okay, we have a model and we have an ID here. Now you notice how with delete, you have got a parameter. And this is actually pretty awesome of a ye. If you have this parameter in your function, that has to actually be in the route. Meaning if you do a delete with no parameter, it's going to say, hey, can't help you. So anyways, it takes this parameter and it says find model ID and delete. So what find model does is, well, it finds the model. It says pets, find one, and that ID is of course the primary key. And we're setting that to null by default. So if it can't find it, it's just going to return null. If it can't do anything, it's going to say, hey, not found, exemption. So you'll get a big fat, you know, 404 page does not exist. So why is all of this important? Well, it's important because this is how your users interact with your site. And this is actually the bulk of the model view controller framework. So like what we're going to do here is we're going to say public, function, action, and we're going to say test. And we're just going to test this out. And we are going to return, we're going to return a render of test. So if we go to the test view or if we put in test for the route, as I should say, what do you think is going to happen here? Invalid parameter. The view file does not exist. And it tells you exactly what we need to do. It tells you exactly where it's looking for that file. So we need to go out here and actually create that view file. So what I'm trying to demonstrate here is that even if you make mistakes, Yee's going to be pretty friendly towards you. So we're going to go here, we're going to say test. And we're just going to say echo, hello world. Why not? Just because it's been that kind of day. And if we go out here now and we do this, boom, there is our custom route right there. So that was how you actually go in and use the controller to make a custom route. Now you may be asking yourself, well, this menu here, I want to put pets up here. How do we do that? How do we make that happen? Well, that's actually not too hard. We're going to go to maybe, if I can find it, layouts, main. Remember, this is our main layout that controls the whole thing. And it should be pretty, pretty simple what's going on here. I mean, you should look at this and that light bulb goes off in your head and goes, I see what's going on. So we're just going to grab this and we're going to put it after the about page here. And all this is, is a navigation widget. It's just that menu. And it has many, sorry, labels embedded in it. So we're going to give the label a test. Give the label a test, that made no sense. Give the label a text of test. And the route, of course, we want to be pets and test. Now when we go to our page here, you'll see not only does it add it, but it's currently highlighted. So if we go to about, about's highlighted and test is highlighted, if we click back. It does that by checking the current route in the controller. If I can find it here. Yeah, route. It basically analyzes the route and says, is the route this? Yes, then highlighted. If not, then don't highlight it. And there's other tweaks you can do that we may touch on in future tutorials. So that is kind of in a nutshell what the controller class does and why it exists. And you notice how now when we do this, it doesn't highlight it. So that's something we can fix in the future tutorial here. Let's flip back into the, I kind of want to go over this controller just so you get a real feel for what's going on here. So we've covered fine model. We've covered the actions. Now let's look at delete here. A couple of things about delete that makes it special. First, we've got a verb, meaning it's going to do a post instead of a get. What that means essentially is, you have to click a form button. You can't just do it via query string. Like you can't say, you know, delete, oops, and ID equal one. You can't do this. See, it says method not allowed 405. This or it can only be handled the following request post. That's what that verb does. It really determines, you know, what you can and can't do. And the reason for that is well twofold. You don't want people keying things in here automatically and trying to delete records. You want them actually interacting with the form. Another thing that is kind of interesting about it is, it has this parameter. And notice how it's got the parameter here. So let's take that parameter out and see what happens. Now, first things first, it didn't do a post. So it's going to catch it there. The point that I filled miserably to illustrate here is that if that parameter is missing, you're going to get a 404 not found because it just won't let you do it. Or it'll give you a 405 method not allowed because you're missing the parameter. So that is, it may seem like it's complex, but once you look at it, it's actually very simple. Pretty elegant what controllers do. The behaviors section. Well, it's, you know, self-explanatory. It's the behavior of the controllers, how it's going to behave. So like if we look at the sites here, we can kind of play around with this. You notice how log out has this at ampersand symbol. Did I just say ampersand? I did. Oh man, that's embarrassing. No, it has the at symbol. And what that means, that's shorthand for authenticated user. By default, if you don't have that, well, then it's going to say, hey, you need to be, you know, logged in. Or I'm sorry, if you don't have it, it'll trust everybody, but because we have it, did I just confuse everybody? I did. I apologize. Let me start over. It's been a long day. You see where it says log out right here? What we're saying is, when you go to log out, you have to be authenticated. I mean, you had to have previously logged in in order to do that. If you're not logged in, it's going to take you automatically to the login page right here. So let's actually do this demo, demo. Notice how we're now logged in. And if we go to log out, it pops us right back to the index, because that's what the log out does. Let's do that again. Demo, demo. And I don't know if the video's picking up, but in the lower left-hand corner down here, you'll see it over by my company when it mouse over. You'll see that route. It's site log out. So I'm going to just click here, and then I'm going to say log out. And boop, not allowed, because it's got to be via a post. Let's go investigate this bad boy here. Sure enough, there's our verb. So let's take this verb out just for the sake of argument here. We're just going to play with this a little bit and see if we can break it, which I'm actually pretty good at breaking things. Ta-da! Notice how we logged out. Now, if we try doing that again, notice how we are logged out, but if let's try logging out again. Notice how the first thing it wants you to do is to log in. It may seem counter-intuitive because we're trying to log out, but you get what I'm saying. Is because we have marked that in the behaviors as authenticated users, you can't do that. So let's kind of put this back the way it was, and we're going to toy around with our PetsController class here, and let's close some of this garbage here. Oh, no. What have I done? There we go. Gosh, scared myself for a minute. All right, so here's our PetsController. You can see we've got our behaviors. Now, if you're doing this on a new class, you should know that you're going to need the verb filter. All right, so let's just take our behaviors here. I'm going to copy that whole thing, and we've got DeletePost, and I'm just going to put this here just so we can have our little copy and paste magic while we're doing this. So we want our access here, and we're just going to copy and paste that right there, and now we can get rid of this. Now, notice how we're calling access. So we need to go in here, and we need to grab Filters Access Control. Otherwise, we're going to get a big, fat error message when we try doing that. So whenever you're working with things, always look for the first item in the array. You notice how Yee is very much array-based, and what we're doing here is we're going to say, Test, you must be authenticated. So we're going to go to, oops, Login, Logout, where was it? Yes, so when we click Test, it automatically wants us to log in. So let's just click out, click Test. Notice how it wants us to log in. Let's go ahead and log in here, and ta-da, we're logged in. We can go to Test because we're logged in. So let's do that again just to follow the flow here. I know this is a bit confusing, so I know I'm going to get a bunch of questions, but basically what Behaviors does, controls the behavior of the controller. We've got some verbs, meaning what sort of actions we're going to take place. Don't confuse the verb actions with the route actions. That can be kind of confusing, but they're totally different things. We're saying Delete has to be a post. So for this Login portion, we're looking at this Access. We're saying Access Control, we're going to use the Access Control class name, or the Access Control class right here. We're saying only on Test, but we can apply it to other things. Actions, Test. Now, this is the route actions. That's why I said, don't get confused between these two. So on Test, we're going to say, Allow True. So we're going to allow A, the Assemble Authenticated Users. That's why when we go here, if we're not logged in, it automatically sends you to log in. That's Basic Access Control and Yee. If we were to flip this to Star, which we really don't need to do, because it's in there by default, that allows anybody to hit that. So we can actually just go click, boop. Notice how a call to member function, check access on null. Don't want to do that. That's why I kind of demonstrated that is, that's a common misconception and common pitfall with Yee. I've done it 100,000 times. It drives me crazy. And the reason why is because in the previous version of Yee, you could do that where you'd mix and match authenticated, non-authenticated. Everything's non-authenticated or star by default. If you want to authenticate, you got to do the ampersand. All right, so put that back the way it was. And ta-da! If we go ahead and log in here, well, demons, what? I was thinking about something else, apparently. There it goes. A little bit confusing, I know. I hope you got the gist of that. When in doubt, really read up on the controller class and what it does. But if you're just kind of like, your eyes are glazing over and you're like, I don't get it, what did we just talk about? All you really need to know is that the controller controls the flow of data in and out. So when you have a route, like we'll say the pets test, where'd you go? There it is. When you have our route, all we're doing is this is what we want to do. And we're going to render a file. We could also, you know, grab a model and create some things and do some stuff. So that's all for this tutorial. I hope you found this educational and entertaining. And I hope it wasn't too confusing, because trust me, this is a confusing topic.