1 00:00:00,000 --> 00:00:02,150 [Seminar] [Kohana: A Lightweight PHP Framework] 2 00:00:02,150 --> 00:00:04,000 [Brandon Liu] [Harvard University] 3 00:00:04,000 --> 00:00:07,270 [This is CS50.] [CS50.TV] 4 00:00:07,270 --> 00:00:09,130 >> Hi everyone. My name is Brandon. 5 00:00:09,130 --> 00:00:11,000 I'm a junior here at the college doing computer science, and today we're going to talk about 6 00:00:11,000 --> 00:00:14,460 Kohana, which is a PHP web development framework. 7 00:00:14,460 --> 00:00:17,260 Today is going to be a live coding seminar, 8 00:00:17,260 --> 00:00:21,000 so I'm basically going to spend 5-10 minutes explaining what Kohana is, 9 00:00:21,000 --> 00:00:25,000 and then I'm literally going to build a super simple blog 10 00:00:25,000 --> 00:00:28,570 for you right here literally from scratch. 11 00:00:28,570 --> 00:00:32,229 We're going to download the code from the Kohana website, 12 00:00:32,229 --> 00:00:35,000 and we're going to start building a blog, and hopefully it'll be very instructive, 13 00:00:35,000 --> 00:00:37,000 because you'll see perhaps maybe I'll make some mistakes, 14 00:00:37,000 --> 00:00:39,000 and you'll see me recover from them, 15 00:00:39,000 --> 00:00:42,040 or you'll see my thought process as I build through this blog, 16 00:00:42,040 --> 00:00:44,000 and meanwhile, you'll also get familiar with the framework itself. 17 00:00:44,000 --> 00:00:48,000 Hopefully it'll be a very instructive exercise. 18 00:00:48,000 --> 00:00:51,370 >> First, what exactly is a framework? 19 00:00:51,370 --> 00:00:54,000 If you've been taking CS50 so far, you haven't really worked with any 20 00:00:54,000 --> 00:00:56,000 frameworks yet, and the thing is this. 21 00:00:56,000 --> 00:01:00,000 You've probably done 1 web development pset already, 22 00:01:00,000 --> 00:01:03,000 and let's say you continue to build websites and keep building websites. 23 00:01:03,000 --> 00:01:05,000 You'll start to notice a few things. 24 00:01:05,000 --> 00:01:07,000 The first thing you'll notice is that you're probably doing the same things 25 00:01:07,000 --> 00:01:09,150 over and over again all the time, 26 00:01:09,150 --> 00:01:13,000 things like cleaning user input data, 27 00:01:13,000 --> 00:01:17,250 things like organizing your files in a certain way. 28 00:01:17,250 --> 00:01:19,000 The other thing you'll also probably notice is that your code 29 00:01:19,000 --> 00:01:21,000 may start to become very, very messy, 30 00:01:21,000 --> 00:01:24,000 and you may leave it messy and just have a very hard time maintaining it, 31 00:01:24,000 --> 00:01:26,000 or you may start to structure your code and making it modular 32 00:01:26,000 --> 00:01:28,510 in certain ways to make it more maintainable. 33 00:01:28,510 --> 00:01:31,020 >> This is where web frameworks basically came in. 34 00:01:31,020 --> 00:01:33,680 These people who had built a lot of websites, they said, 35 00:01:33,680 --> 00:01:35,820 "We don't need to redo this every single time we build a website." 36 00:01:35,820 --> 00:01:39,050 "Why don't we just make a package that does all these things for you 37 00:01:39,050 --> 00:01:41,250 every single time you want to build a website?" 38 00:01:41,250 --> 00:01:43,030 And so that when you make a new website, 39 00:01:43,030 --> 00:01:45,880 you just focus on what exactly this particular website is about. 40 00:01:45,880 --> 00:01:48,630 You don't need to repeat all the various configuration 41 00:01:48,630 --> 00:01:53,070 and separation of codes and rewriting code 42 00:01:53,070 --> 00:01:56,180 that you often have to do when you're making websites. 43 00:01:56,180 --> 00:01:59,410 The idea is that a framework allows you to write a higher-level code 44 00:01:59,410 --> 00:02:01,030 without having to worry about lower-level details. 45 00:02:01,030 --> 00:02:03,670 A lower-level detail might be something like 46 00:02:03,670 --> 00:02:07,250 dealing with cleaning user input data. 47 00:02:07,250 --> 00:02:09,320 That's something that you shouldn't really need to worry about. 48 00:02:09,320 --> 00:02:12,370 You should focus on what your web allocation is actually about. 49 00:02:12,370 --> 00:02:14,790 It eliminates a lot of boilerplate code. 50 00:02:14,790 --> 00:02:16,640 It is an architecture for your project. 51 00:02:16,640 --> 00:02:18,680 >> The most popular one would be Model-View-Controller, 52 00:02:18,680 --> 00:02:20,380 which I'm going to talk about in a second. 53 00:02:20,380 --> 00:02:22,790 And a lot of times these frameworks embody a set of procedures, 54 00:02:22,790 --> 00:02:25,750 rules, and best practices for you to use 55 00:02:25,750 --> 00:02:28,190 so that when you adopt the web framework 56 00:02:28,190 --> 00:02:30,050 you have to write your code in a certain way, 57 00:02:30,050 --> 00:02:32,430 and it's generally an agreed upon set of principles 58 00:02:32,430 --> 00:02:34,290 by the community that is generally accepted to be 59 00:02:34,290 --> 00:02:36,230 a good way of writing code. 60 00:02:36,230 --> 00:02:38,260 It makes your code more maintainable, more usable, 61 00:02:38,260 --> 00:02:40,030 so on and so forth. 62 00:02:40,030 --> 00:02:42,740 And finally, the thing I want to emphasize about frameworks 63 00:02:42,740 --> 00:02:45,980 versus libraries is this idea about inversion of control, and the thing is this. 64 00:02:45,980 --> 00:02:48,430 The difference between a library and a framework is that with a library 65 00:02:48,430 --> 00:02:51,990 you are still writing the main program, 66 00:02:51,990 --> 00:02:54,460 and you're sort of invoking the library 67 00:02:54,460 --> 00:02:56,810 and calling upon the library to do something for you. 68 00:02:56,810 --> 00:02:58,180 The difference between a library and a framework 69 00:02:58,180 --> 00:03:01,620 is that the framework starts out with the control, 70 00:03:01,620 --> 00:03:03,260 and it invokes your code, 71 00:03:03,260 --> 00:03:06,190 so you can think of it as--this is why it's called a framework-- 72 00:03:06,190 --> 00:03:09,700 the framework provides this frame and structure for your code, 73 00:03:09,700 --> 00:03:11,320 and you fill in the holes, 74 00:03:11,320 --> 00:03:13,990 and this will become more apparent in a second 75 00:03:13,990 --> 00:03:16,670 when you see me start to write code within the context of the framework. 76 00:03:16,670 --> 00:03:19,130 You'll see that I'm filling in the gaps, 77 00:03:19,130 --> 00:03:22,450 and the framework is kind of controlling all the moving pieces, 78 00:03:22,450 --> 00:03:27,420 and I have to put the pieces in the right places. 79 00:03:27,420 --> 00:03:29,360 >> Today we're going to talk about Kohana, 80 00:03:29,360 --> 00:03:31,610 which is one of many PHP frameworks. 81 00:03:31,610 --> 00:03:34,550 There are web frameworks, and there are ones in virtually every single language, 82 00:03:34,550 --> 00:03:38,330 and I'm picking Kohana because Kohana is arguably 83 00:03:38,330 --> 00:03:43,250 and generally recognized as the easiest PHP framework to pick up. 84 00:03:43,250 --> 00:03:45,940 It's the most lightweight. 85 00:03:45,940 --> 00:03:48,470 There are other ones out there that come with many, many more features, 86 00:03:48,470 --> 00:03:51,590 but they tend to be more difficult to pick up. 87 00:03:51,590 --> 00:03:54,920 And finally, Kohana uses the MVC architecture. 88 00:03:54,920 --> 00:03:57,780 It's lightweight enough that we can literally build a project 89 00:03:57,780 --> 00:03:59,620 right here right in front of your eyes, and you can pretty much 90 00:03:59,620 --> 00:04:02,780 follow along pretty easily. 91 00:04:02,780 --> 00:04:04,420 >> What is the MVC architecture? 92 00:04:04,420 --> 00:04:06,540 It stands for Model-View-Controller, 93 00:04:06,540 --> 00:04:09,560 and maybe if you think about the code you've been writing so far 94 00:04:09,560 --> 00:04:11,280 for some of your web development psets 95 00:04:11,280 --> 00:04:14,710 you may be able to see some of this, but usually when you start writing 96 00:04:14,710 --> 00:04:16,500 a more complex web application, 97 00:04:16,500 --> 00:04:21,209 the division between these 3 segments becomes more and more evident. 98 00:04:21,209 --> 00:04:26,740 I laid out the MVC here sort of as a stack, 99 00:04:26,740 --> 00:04:29,920 and often you'll hear people talk about stacks in web development, 100 00:04:29,920 --> 00:04:33,760 and this is to illustrate the idea that 101 00:04:33,760 --> 00:04:37,510 each layer, each component really tries to only communicate 102 00:04:37,510 --> 00:04:40,750 between 2 other components. 103 00:04:40,750 --> 00:04:44,550 Someone accesses your website as a client or a browser. 104 00:04:44,550 --> 00:04:48,870 They interact with your program through the view code. 105 00:04:48,870 --> 00:04:50,500 The view code interacts with the controller. 106 00:04:50,500 --> 00:04:52,400 The controller interacts with the model, 107 00:04:52,400 --> 00:04:55,090 and the model interacts with the SQL database. 108 00:04:55,090 --> 00:04:59,670 And there is no hopping in between if you write your code properly. 109 00:04:59,670 --> 00:05:01,670 >> What do these things do? 110 00:05:01,670 --> 00:05:06,020 The model essentially is the piece of code that deals with your data. 111 00:05:06,020 --> 00:05:09,100 Anything that deals with your database, with the objects that you store, 112 00:05:09,100 --> 00:05:11,280 or retrieving those objects in the database, 113 00:05:11,280 --> 00:05:13,450 that's all handled by the model. 114 00:05:13,450 --> 00:05:15,290 Maybe you have objects in your database. 115 00:05:15,290 --> 00:05:17,700 We're going to create a model having to do with posts, 116 00:05:17,700 --> 00:05:19,760 so a post may have some attributes to it. 117 00:05:19,760 --> 00:05:23,900 You may have functions around storing those posts or retrieving posts 118 00:05:23,900 --> 00:05:26,270 or filtering the posts and so on and so forth, 119 00:05:26,270 --> 00:05:28,880 and that's all the code that's handled by the model. 120 00:05:28,880 --> 00:05:31,780 The controller is sort of the application logic, 121 00:05:31,780 --> 00:05:37,910 and a lot of different things can go in the application logic. 122 00:05:37,910 --> 00:05:39,790 If you're talking to a different API, 123 00:05:39,790 --> 00:05:42,310 that may be where you're dealing with the application logic. 124 00:05:42,310 --> 00:05:47,990 If you're trying to have to bring in data from multiple different models 125 00:05:47,990 --> 00:05:51,540 and have to combine them in some way, that often may be handled by the controller. 126 00:05:51,540 --> 00:05:53,820 For example, on Facebook, if you friend someone, 127 00:05:53,820 --> 00:05:58,500 then perhaps that act of establishing that relationship 128 00:05:58,500 --> 00:06:00,490 may be done by the controller. 129 00:06:00,490 --> 00:06:04,350 >> And finally, the view is the code that's generating what you actually see. 130 00:06:04,350 --> 00:06:07,410 A lot of times I think in the CS50 psets 131 00:06:07,410 --> 00:06:10,050 they don't really encourage you guys to separate these 3 things. 132 00:06:10,050 --> 00:06:12,430 You'll probably have this big, long file where at the top 133 00:06:12,430 --> 00:06:15,130 you make some SQL query and maybe do some processing 134 00:06:15,130 --> 00:06:17,020 on the data you retrieved from the database, 135 00:06:17,020 --> 00:06:19,370 and then you have all your HTML at the bottom. 136 00:06:19,370 --> 00:06:22,840 And you may find that as you create more and more pages 137 00:06:22,840 --> 00:06:25,790 that you're going to have some code repetition, and also, 138 00:06:25,790 --> 00:06:29,600 the thing is your file gets really big and long 139 00:06:29,600 --> 00:06:32,370 and becomes unwieldy to manage. 140 00:06:32,370 --> 00:06:34,500 The reason why MVC is so well regarded 141 00:06:34,500 --> 00:06:36,120 is for a number of reasons. 142 00:06:36,120 --> 00:06:38,110 The first thing is something called separation of concerns 143 00:06:38,110 --> 00:06:40,370 which is the idea that when you have-- 144 00:06:40,370 --> 00:06:43,520 ideally 1 piece of code should do 1 thing and do it really well, 145 00:06:43,520 --> 00:06:47,210 and you shouldn't combine pieces of code that do disparate things. 146 00:06:47,210 --> 00:06:49,230 For example, view code and model code, 147 00:06:49,230 --> 00:06:51,040 they don't really have to be related. 148 00:06:51,040 --> 00:06:53,290 They don't have to be in the same files, so when you can, separate them out 149 00:06:53,290 --> 00:06:55,300 so it's easy to maintain. 150 00:06:55,300 --> 00:06:57,130 >> The other thing is code reuse. 151 00:06:57,130 --> 00:06:59,770 You may find yourself writing the same SQL query or doing 152 00:06:59,770 --> 00:07:03,060 similar queries that could be abstracted into 1 function, 153 00:07:03,060 --> 00:07:05,440 and that's the idea behind models and controllers, 154 00:07:05,440 --> 00:07:09,690 having it in a separate function that you can reuse in different places in your project. 155 00:07:09,690 --> 00:07:12,420 And finally, that's tied to DRYing your code, 156 00:07:12,420 --> 00:07:14,700 or not repeating yourself, don't repeat yourself. 157 00:07:14,700 --> 00:07:18,090 This is very comprehensible in development. 158 00:07:18,090 --> 00:07:20,110 Whenever you can, you don't want to repeat yourself, because if you repeat yourself, 159 00:07:20,760 --> 00:07:23,400 it's much more costly to maintain. 160 00:07:23,400 --> 00:07:26,040 If you want to change 1 thing, you have to change it everywhere, 161 00:07:26,040 --> 00:07:30,230 and that leads to bugs, and it's horrible. 162 00:07:30,230 --> 00:07:32,010 >> All right. 163 00:07:32,010 --> 00:07:37,270 Any questions so far about Kohana at all? 164 00:07:37,270 --> 00:07:39,200 Great. 165 00:07:39,200 --> 00:07:42,300 Now we're going to dive into the live coding session, 166 00:07:42,300 --> 00:07:48,050 and hopefully everything goes well. 167 00:07:57,200 --> 00:08:00,050 I am going to basically build this website 168 00:08:00,050 --> 00:08:03,910 on one of my remote servers, and that way you guys can also 169 00:08:03,910 --> 00:08:06,310 see the website and access the website, 170 00:08:06,310 --> 00:08:10,990 and also the environment is better configured than my remote machine, 171 00:08:10,990 --> 00:08:14,580 because it's running Linux instead of OS X. 172 00:08:14,580 --> 00:08:16,260 We're literally going to start. 173 00:08:16,260 --> 00:08:18,110 KohanaFramework.org. 174 00:08:18,110 --> 00:08:21,350 I'm going to download the code from the website. 175 00:08:21,350 --> 00:08:24,610 I'm going to copy the link address, 176 00:08:24,610 --> 00:08:29,770 go to my server, download it, 177 00:08:29,770 --> 00:08:33,000 and I'm going to extract it. 178 00:08:38,330 --> 00:08:45,710 >> [Student] What's the largest you can make the text? 179 00:08:45,710 --> 00:08:47,330 [Brandon Liu] Is that better? 180 00:08:47,330 --> 00:08:50,070 [Student] Is that doable?>>[Brandon Liu] Yeah, that's fine. 181 00:08:50,070 --> 00:08:54,500 I downloaded a ZIP file and unzipped that into a directory called Kohana, 182 00:08:54,500 --> 00:08:58,820 and we're going to rename that CS50-Kohana, 183 00:08:58,820 --> 00:09:01,140 and let's go in. 184 00:09:01,140 --> 00:09:03,610 Awesome. 185 00:09:03,610 --> 00:09:06,870 Here you see a bunch of different files. 186 00:09:06,870 --> 00:09:10,140 Most of you can ignore--we're not going to go through every single file that's in here 187 00:09:10,140 --> 00:09:13,130 because of our time constraints, 188 00:09:13,130 --> 00:09:16,310 but generally when you install Kohana, the first thing you do 189 00:09:16,310 --> 00:09:23,210 is you go to the directory, 190 00:09:23,210 --> 00:09:26,050 and you'll basically do some environment tests and whatnot 191 00:09:26,050 --> 00:09:28,640 to make sure your environment is properly set to run Kohana 192 00:09:28,640 --> 00:09:31,450 and make sure that everything is all right. 193 00:09:31,450 --> 00:09:35,510 You can see most things passed, but generally you always run into this 1 problem 194 00:09:35,510 --> 00:09:38,180 where it complains that some directory is not writable, 195 00:09:38,180 --> 00:09:40,410 and that's because of some permissions. 196 00:09:40,410 --> 00:09:43,080 I don't know how much you guys have learned about file permissions in CS50, 197 00:09:43,080 --> 00:09:47,920 but if you do web development, you're going to run into this issue a lot. 198 00:09:47,920 --> 00:09:58,340 I'm going to make it writable 199 00:09:58,340 --> 00:10:03,390 and I think I also have to--there we go. 200 00:10:03,390 --> 00:10:07,040 >> Okay, so now you can see everything passed, 201 00:10:07,040 --> 00:10:10,000 and now it will tell you to rename the install.php file. 202 00:10:10,000 --> 00:10:15,630 I'm going to move the install.php file to installed.php, 203 00:10:15,630 --> 00:10:19,610 and now if I refresh, 204 00:10:19,610 --> 00:10:22,810 it gives me some error, and this is where the debugging comes in. 205 00:10:22,810 --> 00:10:25,610 This is where you can see what's actually going to happen. 206 00:10:25,610 --> 00:10:28,460 The thing is, by default, Kohana assumes that 207 00:10:28,460 --> 00:10:31,480 your project is at the root directory of your domain, 208 00:10:31,480 --> 00:10:35,920 so it's expecting you to be at demo.brandonkliu.com. 209 00:10:35,920 --> 00:10:37,540 We have to tell it that it's actually in a subfolder. 210 00:10:37,540 --> 00:10:39,820 It's in a subfolder called CS50 Kohana. 211 00:10:39,820 --> 00:10:42,640 The thing is, it's misinterpreting CS50-Kohana 212 00:10:42,640 --> 00:10:45,680 as something else, which I'll explain to you in a second. 213 00:10:45,680 --> 00:10:49,910 But I should tell you that's something that's to be expected. 214 00:10:49,910 --> 00:10:53,700 What we're going to do is we're going to go into this folder called bootstrap.php, 215 00:10:53,700 --> 00:10:59,260 which is the configuration folder where a lot of different things are set up. 216 00:10:59,260 --> 00:11:01,570 I open that up. 217 00:11:01,570 --> 00:11:09,790 Then maybe one of the first things I'll do is change the time zone. 218 00:11:09,790 --> 00:11:13,910 >> And then let's see. 219 00:11:13,910 --> 00:11:15,180 Aha! Right here. 220 00:11:15,180 --> 00:11:18,690 There are a bunch of different configuration sayings in here, 221 00:11:18,690 --> 00:11:21,700 but the one I'm looking for is this thing called base URL, 222 00:11:21,700 --> 00:11:24,570 and by default I get it set to Kohana, 223 00:11:24,570 --> 00:11:29,020 but I'm going to change that to CS50-Kohana, 224 00:11:29,020 --> 00:11:32,880 and I think that should fix it. 225 00:11:32,880 --> 00:11:34,150 Yes, great. 226 00:11:34,150 --> 00:11:39,530 By default, to see that it's working, it says, "Hello World." 227 00:11:39,530 --> 00:11:42,930 Where did that come from? How did we get to Hello World? 228 00:11:42,930 --> 00:11:47,640 Where exactly is the code that actually wrote that? 229 00:11:47,640 --> 00:11:50,240 To understand that, I'll introduce this concept called routing. 230 00:11:50,240 --> 00:11:52,590 Pretty much all web frameworks have the concept called routing, 231 00:11:52,590 --> 00:11:57,230 which is the piece of the software that will map a certain URL 232 00:11:57,230 --> 00:12:01,550 to a certain piece of code within your framework. 233 00:12:01,550 --> 00:12:07,510 For example, if you have some URL and you go to some URL like foo.com/blog/all 234 00:12:07,510 --> 00:12:11,170 then what the framework is going to do--or at least what Kohana is going to do-- 235 00:12:11,170 --> 00:12:15,540 is it's going to find a class called controller blog, 236 00:12:15,540 --> 00:12:18,720 and it's going to run the function named action all. 237 00:12:18,720 --> 00:12:20,160 I know I'm talking about class and functions, 238 00:12:20,160 --> 00:12:23,860 and I know you guys haven't covered classes and functions 239 00:12:23,860 --> 00:12:26,470 in CS50 yet, but for now, 240 00:12:26,470 --> 00:12:29,800 you can think of classes as just a group of functions, 241 00:12:29,800 --> 00:12:32,900 a way of grouping functions together. 242 00:12:32,900 --> 00:12:37,690 That's really all you need to know. 243 00:12:37,690 --> 00:12:43,120 >> Now if we look at our folder structure, 244 00:12:43,120 --> 00:12:47,110 inside the application folder there is another folder called classes, 245 00:12:47,110 --> 00:12:49,200 and the other folders are called Controller and Model. 246 00:12:49,200 --> 00:12:52,530 If you look inside the Controller folder, 247 00:12:52,530 --> 00:12:56,330 we see that there is a file called Welcome, 248 00:12:56,330 --> 00:13:00,370 and you can see here is a class called Controller Welcome, 249 00:13:00,370 --> 00:13:02,340 and there is a function called Action Index, 250 00:13:02,340 --> 00:13:06,360 and what it does is it sets the body of your response to Hello World. 251 00:13:06,360 --> 00:13:08,730 That's where the code is being written. 252 00:13:08,730 --> 00:13:11,610 The other question is, well, I didn't go to 253 00:13:11,610 --> 00:13:13,680 blah, blah, blah, /welcome/index. 254 00:13:13,680 --> 00:13:16,250 How did I end up here? 255 00:13:16,250 --> 00:13:20,410 Well, that's simply because 256 00:13:20,410 --> 00:13:22,410 here at the bottom of our bootstrap file 257 00:13:22,410 --> 00:13:24,550 where we set our routes 258 00:13:24,550 --> 00:13:26,570 you can see that they set some defaults for you. 259 00:13:26,570 --> 00:13:28,990 The default controller is Welcome. The default action is Index. 260 00:13:28,990 --> 00:13:31,600 That's why when we put nothing in there it automatically went to the Welcome controller 261 00:13:31,600 --> 00:13:33,940 and the index Action. 262 00:13:33,940 --> 00:13:37,460 >> Everything make sense so far? 263 00:13:37,460 --> 00:13:40,850 Now, you can do more than just go to Controller 264 00:13:40,850 --> 00:13:43,670 and a specific action. 265 00:13:43,670 --> 00:13:48,480 You can also pass in parameters to the controller. 266 00:13:48,480 --> 00:13:56,390 Just as an example, 267 00:13:56,390 --> 00:14:04,070 I'm going to add another action to this controller to show you. 268 00:14:04,070 --> 00:14:09,130 Let's call this action Echo, because it's going to tell you whatever you give it, 269 00:14:09,130 --> 00:14:17,510 and so I'm basically going to grab 270 00:14:17,510 --> 00:14:20,960 a parameter that's going to be sent through me to the routing program, 271 00:14:20,960 --> 00:14:24,440 and as you can see here, 272 00:14:24,440 --> 00:14:28,440 this line right here, you can see that 273 00:14:28,440 --> 00:14:31,270 this basically means you have controller, and you have a /, 274 00:14:31,270 --> 00:14:33,480 and you have action, and you have another /, 275 00:14:33,480 --> 00:14:39,140 and that's going to be parameters, and because we have this name ID 276 00:14:39,140 --> 00:14:42,450 within angle brackets, that means that we're naming this parameter ID. 277 00:14:42,450 --> 00:14:45,490 Later in my controller code if I want to grab a hold of that parameter, 278 00:14:45,490 --> 00:14:51,790 I can use the code I wrote, find the parameter named ID. 279 00:14:51,790 --> 00:14:59,290 That's what I did here, and I'm going to return and say, 280 00:14:59,290 --> 00:15:07,090 "You said" that. 281 00:15:07,090 --> 00:15:10,760 And so now if I go to our website, 282 00:15:10,760 --> 00:15:19,550 I go to cs50-kohana/welcome/echo/Helloooo-- 283 00:15:19,550 --> 00:15:21,420 oh, that's right. 284 00:15:21,420 --> 00:15:23,930 There is 1 step I left out. 285 00:15:23,930 --> 00:15:27,020 This is part of the live coding idea. 286 00:15:27,020 --> 00:15:36,100 >> Here's 1 thing. Let's see. 287 00:15:36,100 --> 00:15:38,870 So normally by default with a lot of these web applications 288 00:15:38,870 --> 00:15:42,820 you have to include this index.php thing in your URL, 289 00:15:42,820 --> 00:15:46,410 because the idea is index.php is sort of the entry point 290 00:15:46,410 --> 00:15:48,250 of your application, but of course, 291 00:15:48,250 --> 00:15:50,110 that's sort of annoying to have. 292 00:15:50,110 --> 00:15:53,790 You don't want to have index.php appear in your URL, 293 00:15:53,790 --> 00:15:56,080 and pretty much every web framework out of the box 294 00:15:56,080 --> 00:15:58,440 has this index.php problem, 295 00:15:58,440 --> 00:16:03,370 and so you have to take some measures to be able to remove that. 296 00:16:03,370 --> 00:16:07,540 And so in this case, 297 00:16:07,540 --> 00:16:11,450 what we're going to do is we're going to use a file called .htaccess, 298 00:16:11,450 --> 00:16:13,900 and this is something that's specific to the Apache web server, 299 00:16:13,900 --> 00:16:16,290 and it can do things like rewrite URLs 300 00:16:16,290 --> 00:16:19,350 and redirect URLs and so on and so forth, 301 00:16:19,350 --> 00:16:24,280 and Kohana is nice enough to provide a template .htaccess file that we can use. 302 00:16:24,280 --> 00:16:28,300 >> As you can see, there is a file there called example.htaccess, 303 00:16:28,300 --> 00:16:33,410 and we're going to copy that to .htaccess. 304 00:16:33,410 --> 00:16:36,950 I'm going to open this and edit it, 305 00:16:36,950 --> 00:16:40,840 and basically it does a bunch of different things. 306 00:16:40,840 --> 00:16:45,320 The key line you may want to look at is right here. 307 00:16:45,320 --> 00:16:49,840 The idea is that this sets up a rule that says, 308 00:16:49,840 --> 00:16:56,400 "Okay, whatever you type in, prepend index.php to that." 309 00:16:56,400 --> 00:16:58,710 You can see that. 310 00:16:58,710 --> 00:17:00,370 The .* stands for anything, match anything, 311 00:17:00,370 --> 00:17:03,300 and then the second part is index.php/$0, 312 00:17:03,300 --> 00:17:07,410 and $0 refers to whatever was matched previously. 313 00:17:07,410 --> 00:17:09,500 Does that make sense? 314 00:17:09,500 --> 00:17:12,190 But the really key thing I want to change is change this rewrite base, 315 00:17:12,190 --> 00:17:14,300 which is the URL base. 316 00:17:14,300 --> 00:17:17,780 It sort of assumes where you're working from. 317 00:17:17,780 --> 00:17:22,560 I'm going to add CS50 Kohana to that, 318 00:17:22,560 --> 00:17:26,530 and that way now if I remove the index.php, 319 00:17:26,530 --> 00:17:32,110 it should work, and I'm going to add some numbers 320 00:17:32,110 --> 00:17:36,380 to show you that it indeed did work. 321 00:17:36,380 --> 00:17:38,130 Sounds good. 322 00:17:38,130 --> 00:17:40,260 >> Any questions so far? 323 00:17:40,260 --> 00:17:42,300 [Student] How did it know to make the 123? 324 00:17:42,300 --> 00:17:44,120 Is that an argument? 325 00:17:44,120 --> 00:17:46,560 Exactly. You can think of it just like an argument. 326 00:17:46,560 --> 00:17:52,410 But the weird thing, though, is that the way Kohana does it 327 00:17:52,410 --> 00:17:54,910 is they don't do it exactly like an argument. 328 00:17:54,910 --> 00:17:56,930 You have to grab it like this. 329 00:17:56,930 --> 00:18:01,030 You have to grab the request object and ask for the parameter that's named ID, 330 00:18:01,030 --> 00:18:03,240 and that name ID comes from that bootstrap file 331 00:18:03,240 --> 00:18:06,990 that I showed earlier, and the name ID was in those angle brackets, 332 00:18:06,990 --> 00:18:11,580 and that's how you grab those parameters. 333 00:18:11,580 --> 00:18:14,010 Awesome. 334 00:18:14,010 --> 00:18:17,550 Any other questions? 335 00:18:17,550 --> 00:18:20,500 Like I said, controllers, they handle application logic, 336 00:18:20,500 --> 00:18:22,980 so that's 1 instance where you can see that's-- 337 00:18:22,980 --> 00:18:24,830 it's very basic, but it's still application logic, 338 00:18:24,830 --> 00:18:27,980 the idea of grabbing the parameter and creating a new string 339 00:18:27,980 --> 00:18:31,920 that says, "You said blah," and then spitting that back to you. 340 00:18:31,920 --> 00:18:34,030 And generally what you do is you create different controllers. 341 00:18:34,030 --> 00:18:36,450 You create separate controllers for different parts of your website. 342 00:18:36,450 --> 00:18:38,160 >> Today we're going to make a very simple website, 343 00:18:38,160 --> 00:18:40,420 and it's going to be a very basic blog. 344 00:18:40,420 --> 00:18:43,780 We're going to make a new controller just for the posts in a blog. 345 00:18:43,780 --> 00:18:47,060 But then if I were to also add comments to the blog post, 346 00:18:47,060 --> 00:18:50,140 then I would probably want to make a new controller for those comments. 347 00:18:50,140 --> 00:18:53,380 If I wanted to add users, I would probably add a new controller for those users, 348 00:18:53,380 --> 00:18:57,000 and in general, the idea is that whenever you have a new model, 349 00:18:57,000 --> 00:18:59,630 a new data object that you're dealing with, 350 00:18:59,630 --> 00:19:02,970 you have a single controller for that data object. 351 00:19:02,970 --> 00:19:04,370 Today we're only going to work with 1 data object, 352 00:19:04,370 --> 00:19:06,250 and that's going to be posts, 353 00:19:06,250 --> 00:19:08,710 and also you can think of data objects as corresponding to tables. 354 00:19:08,710 --> 00:19:12,160 Generally each table corresponds to 1 type of data object, 355 00:19:12,160 --> 00:19:15,160 so the post table will have 1 post model, 356 00:19:15,160 --> 00:19:18,230 which will have 1 post controller corresponding to that, 357 00:19:18,230 --> 00:19:22,190 and the same for comments, the same for users, and so on and so forth. 358 00:19:22,190 --> 00:19:24,070 And that's a general rule of thumb. 359 00:19:24,070 --> 00:19:27,460 There are going to be special cases where you may differ from that, 360 00:19:27,460 --> 00:19:29,300 but 90% of the time that's what you're going to be doing, 361 00:19:29,300 --> 00:19:32,810 and I'll show you that's what we're going to be doing today. 362 00:19:32,810 --> 00:19:35,490 1 more concept before we dive back into the code, 363 00:19:35,490 --> 00:19:37,710 this idea of object relational mapping. 364 00:19:37,710 --> 00:19:41,200 >> You guys have already done a web development pset, 365 00:19:41,200 --> 00:19:43,820 and you've seen that you make an SQL query, 366 00:19:43,820 --> 00:19:46,510 and whatever it returns to you are rows. 367 00:19:46,510 --> 00:19:50,040 You get these rows, and you index them by some name, 368 00:19:50,040 --> 00:19:55,480 the name of the column and the table, 369 00:19:55,480 --> 00:19:57,630 and that's how you work with it, 370 00:19:57,630 --> 00:19:59,290 and it can be a bit cumbersome. 371 00:19:59,290 --> 00:20:01,810 But furthermore, if you have relationships within your database, 372 00:20:01,810 --> 00:20:05,280 like for example if I have comments and posts, 373 00:20:05,280 --> 00:20:11,240 then maybe I want to grab the parent post of a comment. 374 00:20:11,240 --> 00:20:14,350 If I use just rows in SQL, then all I can get is the ID 375 00:20:14,350 --> 00:20:19,310 of the parent post and not the actual post itself. 376 00:20:19,310 --> 00:20:21,680 But when we're coding, what we actually want is to actually grab 377 00:20:21,680 --> 00:20:23,550 the parent post itself sometimes. 378 00:20:23,550 --> 00:20:25,730 What object relational mapping does is 379 00:20:25,730 --> 00:20:29,480 it takes the results of the database query 380 00:20:29,480 --> 00:20:32,420 and puts it into objects for you, which are much nicer to work with 381 00:20:32,420 --> 00:20:34,770 than plain arrays and rows. 382 00:20:34,770 --> 00:20:37,550 >> For example, now when I have a comment perhaps, 383 00:20:37,550 --> 00:20:40,900 and I want to grab its parent post, 384 00:20:40,900 --> 00:20:43,440 and I do maybe comment arrow post, 385 00:20:43,440 --> 00:20:45,230 then it will actually give me the post object 386 00:20:45,230 --> 00:20:47,940 corresponding to the actual parent post, not just some ID, 387 00:20:47,940 --> 00:20:52,210 which I would otherwise have to use and make another SQL query to grab the post, 388 00:20:52,210 --> 00:20:57,430 which is cumbersome and unnecessary. 389 00:20:57,430 --> 00:21:01,840 And furthermore, by mapping all these data rows into objects, 390 00:21:01,840 --> 00:21:03,760 you can also attach more functions to objects, 391 00:21:03,760 --> 00:21:09,700 so for example, I talked about how classes are essentially groupings of functions. 392 00:21:09,700 --> 00:21:11,620 You can think of it like that. 393 00:21:11,620 --> 00:21:15,290 For example, maybe I have this post object, 394 00:21:15,290 --> 00:21:17,830 and maybe I'd like to have some sort of function attached to it 395 00:21:17,830 --> 00:21:20,300 that basically tells me was it recently posted? 396 00:21:20,300 --> 00:21:23,570 Was it posted within the last week, true or false? 397 00:21:23,570 --> 00:21:27,320 And that's a function I can attach onto that object, 398 00:21:27,320 --> 00:21:31,300 and it's really convenient to have it in the same place, 399 00:21:31,300 --> 00:21:33,820 and there are a host of different functions you can create 400 00:21:33,820 --> 00:21:37,990 for these objects, and it's really nice to be able to attach it to a class, 401 00:21:37,990 --> 00:21:41,700 to an object, whereas if you just had rows coming from your database, 402 00:21:41,700 --> 00:21:43,790 then you can't really attach any functionality to that. 403 00:21:43,790 --> 00:21:47,850 It's literally just data. 404 00:21:47,850 --> 00:21:50,550 Any questions about that at all? 405 00:21:50,550 --> 00:21:52,710 ORMs are very common web development, 406 00:21:52,710 --> 00:21:56,330 and there are a lot of different types of ORMs, 407 00:21:56,330 --> 00:21:58,450 and Kohana has its own ORM. 408 00:21:58,450 --> 00:22:05,050 It's very basic, but you'll get a taste of what it looks like. 409 00:22:05,050 --> 00:22:08,780 >> Let's create a model for our blog posts, 410 00:22:08,780 --> 00:22:12,350 and the first thing we obviously need to do is to create an actual table 411 00:22:12,350 --> 00:22:16,680 within our database to actually store our data for those posts. 412 00:22:16,680 --> 00:22:19,260 The first thing I'm going to do is go to phpMyAdmin. 413 00:22:19,260 --> 00:22:21,410 Have you guys used phpMyAdmin before? 414 00:22:21,410 --> 00:22:23,400 Okay, awesome, so you guys already know what that is, 415 00:22:23,400 --> 00:22:32,200 and I'm going to create a new table called Kohana Posts, 416 00:22:32,200 --> 00:22:37,820 and it's going to be really simple. 417 00:22:37,820 --> 00:22:40,190 I'll have to log back in. 418 00:23:02,620 --> 00:23:04,640 All we're going to do today is have an author and a body, 419 00:23:04,640 --> 00:23:11,930 just keep it simple. 420 00:23:11,930 --> 00:23:15,620 I'm going to create that table, 421 00:23:15,620 --> 00:23:19,620 and now we just have a table representing our posts 422 00:23:19,620 --> 00:23:23,370 with 2 fields for our author and our body. 423 00:23:23,370 --> 00:23:26,290 The other thing I am going to do now is 424 00:23:26,290 --> 00:23:29,820 configure my web application so it knows how to connect to the database, 425 00:23:29,820 --> 00:23:31,950 and this, again, is something that you'll have to do with all web applications. 426 00:23:31,950 --> 00:23:34,790 You have to tell it the user name and the password 427 00:23:34,790 --> 00:23:36,990 and the name of the database and so on and so forth 428 00:23:36,990 --> 00:23:40,000 to figure out how to actually connect to your database. 429 00:23:40,000 --> 00:23:58,710 >> In Kohana, we have something called a database module, 430 00:23:58,710 --> 00:24:02,690 and in the configuration folder we have this folder called Database, 431 00:24:02,690 --> 00:24:07,330 and as you can see, there are a bunch of settings you have to set here 432 00:24:07,330 --> 00:24:09,860 to tell it what's the user name and the password 433 00:24:09,860 --> 00:24:13,110 for the database so I can actually connect to it. 434 00:24:13,110 --> 00:24:15,010 And since I don't want you guys to actually know 435 00:24:15,010 --> 00:24:17,190 the user name and password of my database, 436 00:24:17,190 --> 00:24:23,840 I have a file where I already set it all up, and I'm going to copy and paste it over. 437 00:24:33,080 --> 00:24:36,870 Awesome. 438 00:24:36,870 --> 00:24:39,880 Okay. I think that's all the configuration I need to do, 439 00:24:39,880 --> 00:24:41,070 but let's see. 440 00:24:41,070 --> 00:24:43,720 We'll keep working in it, and if something crashes, 441 00:24:43,720 --> 00:24:47,490 then we'll fix it. 442 00:24:47,490 --> 00:24:51,830 Now what I'm going to do is I'm going to create a new controller. 443 00:24:51,830 --> 00:24:53,190 Or actually, sorry. 444 00:24:53,190 --> 00:24:55,080 First I have to create a new model. 445 00:24:55,080 --> 00:25:01,620 I'll create a new model called Post.php, 446 00:25:01,620 --> 00:25:12,440 and what we're going to do is we're going to call it class Model_Post. 447 00:25:12,440 --> 00:25:15,390 Get some syntax highlighting on, 448 00:25:15,390 --> 00:25:19,750 and so when I say, "extends ORM," that's basically 449 00:25:19,750 --> 00:25:21,210 some more object-oriented programming, 450 00:25:21,210 --> 00:25:23,340 which unfortunately you guys haven't learned in CS50 yet, 451 00:25:23,340 --> 00:25:25,290 but it's pretty easy to pick up. 452 00:25:25,290 --> 00:25:27,950 It gives me all this extra functionality that comes in this ORM package, 453 00:25:27,950 --> 00:25:31,120 and so I get a bunch of extra functions and whatnot for free, 454 00:25:31,120 --> 00:25:34,810 which you'll see a bit of in a second. 455 00:25:34,810 --> 00:25:37,670 >> Right now actually all I need to do is create this class. 456 00:25:37,670 --> 00:25:39,160 I don't even need to make any function or anything, 457 00:25:39,160 --> 00:25:41,770 but I've created a class that represents the table, 458 00:25:41,770 --> 00:25:44,140 and because I've extended this ORM class, 459 00:25:44,140 --> 00:25:51,080 I get a bunch of things for free, so for now you don't have to set anything more up. 460 00:25:51,080 --> 00:25:53,530 And now what I'm going to do is I'm going to create a new controller, 461 00:25:53,530 --> 00:25:58,480 which I'm going to name blog.php, 462 00:25:58,480 --> 00:26:04,350 and I'm going to copy over the Welcome controller 463 00:26:04,350 --> 00:26:11,950 so I don't have to retype some stuff, 464 00:26:11,950 --> 00:26:20,720 and now I have to rename this. 465 00:26:20,720 --> 00:26:24,710 Now what I'm going to do to test to make sure everything is working out, 466 00:26:24,710 --> 00:26:27,820 I'm going to grab the first post from my database 467 00:26:27,820 --> 00:26:32,680 and print the body of the post on the screen. 468 00:26:32,680 --> 00:26:37,920 To do that what I'm going to do first is I'm going to save the posts 469 00:26:37,920 --> 00:26:48,770 to a variable so what we're going to do is-- 470 00:26:48,770 --> 00:26:52,090 in Kohana what you do is to grab the post object 471 00:26:52,090 --> 00:26:55,380 it's kind of cumbersome, but you have to do this thing called ORM:: factory, 472 00:26:55,380 --> 00:26:57,750 and then you pass in the name of the model you want, 473 00:26:57,750 --> 00:27:00,490 and it returns the ORM object that represents that model. 474 00:27:00,490 --> 00:27:04,860 And then, like I said, when we extend the ORM object, 475 00:27:04,860 --> 00:27:07,320 we get all these methods for free, so for example, 476 00:27:07,320 --> 00:27:09,200 we get this new function called "find all," 477 00:27:09,200 --> 00:27:12,160 which automatically returns every single post in the database, 478 00:27:12,160 --> 00:27:14,850 which is pretty convenient. 479 00:27:14,850 --> 00:27:17,480 >> And now in the body I'm going to return 480 00:27:17,480 --> 00:27:24,860 the first post and return its body. 481 00:27:24,860 --> 00:27:27,930 And of course, I need to create a post, 482 00:27:27,930 --> 00:27:31,880 so let's insert a new post. 483 00:27:31,880 --> 00:27:37,870 I'll say, "Brandon, my very first post." 484 00:27:37,870 --> 00:27:40,010 Awesome. 485 00:27:40,010 --> 00:27:45,910 And now we're going to go to blogs 486 00:27:45,910 --> 00:27:50,960 and if all works well--oh, this is some other dumb file permission thing again. 487 00:27:50,960 --> 00:27:56,090 Hold on 1 second. It's kind of absurd. 488 00:28:06,700 --> 00:28:08,490 There we go. Okay. 489 00:28:08,490 --> 00:28:10,040 I fixed that permission problem. 490 00:28:10,040 --> 00:28:12,040 It was trying to create some files and some log, 491 00:28:12,040 --> 00:28:15,400 and the permissions, again, weren't properly set, so I made it 492 00:28:15,400 --> 00:28:18,320 so those files were writable and executable 493 00:28:18,320 --> 00:28:21,090 so it could actually log to things. 494 00:28:21,090 --> 00:28:24,220 Now it's giving me another exception saying, "class ORM not found," 495 00:28:24,220 --> 00:28:26,960 and that's because I forgot another step. 496 00:28:26,960 --> 00:28:37,010 That's too bad. 497 00:28:37,010 --> 00:28:40,270 In the bootstrap folder file, there are these modules here, 498 00:28:40,270 --> 00:28:42,480 which you can choose to enable or disable. 499 00:28:42,480 --> 00:28:44,340 These are a bunch of different features that you can choose to use 500 00:28:44,340 --> 00:28:46,180 within Kohana, which is sort of nice. 501 00:28:46,180 --> 00:28:49,090 >> For example, they have an authentication module 502 00:28:49,090 --> 00:28:51,170 which you can use for authenticating users. 503 00:28:51,170 --> 00:28:53,390 They have a caching module if you want to implement 504 00:28:53,390 --> 00:28:57,870 some sort of caching back end to make the application work faster and whatnot. 505 00:28:57,870 --> 00:29:02,140 We need to enable the database and the ORM module, 506 00:29:02,140 --> 00:29:04,280 because like I said, we're using the database, obviously, 507 00:29:04,280 --> 00:29:08,200 and we also need to enable the ORM module, 508 00:29:08,200 --> 00:29:12,220 because we'd like to have the extra functionality, which is nice to have. 509 00:29:12,220 --> 00:29:14,240 All I have to do is uncomment those 2 lines, 510 00:29:14,240 --> 00:29:18,760 and now if I refresh, it gave me another error. 511 00:29:18,760 --> 00:29:22,100 It says, "Class Model_Post not found." 512 00:29:22,100 --> 00:29:30,210 Now this is a good problem to have. 513 00:29:30,210 --> 00:29:37,660 Let's see. 514 00:29:37,660 --> 00:29:42,200 Make it public. 515 00:29:42,200 --> 00:29:46,450 No. Hold on. 516 00:30:11,610 --> 00:30:13,160 Oh, dear. 517 00:30:13,160 --> 00:30:18,590 I do not know why it's not able to find that. 518 00:30:18,590 --> 00:30:21,030 That's really strange. 519 00:30:21,030 --> 00:30:23,820 I have this class right here. 520 00:30:23,820 --> 00:30:28,650 I guess I might have to--oh. 521 00:30:28,650 --> 00:30:32,010 I am so dumb. I forgot to add a PHP tag. 522 00:30:32,010 --> 00:30:34,670 That's why. 523 00:30:34,670 --> 00:30:41,260 Now I have to undo that 1 change I just did. 524 00:30:41,260 --> 00:30:44,270 >> Okay. There we go. 525 00:30:44,270 --> 00:30:47,500 That was really silly. I didn't have an opening PHP tag. 526 00:30:47,500 --> 00:30:49,900 But as you can see, now it's working properly, right? 527 00:30:49,900 --> 00:30:51,240 We have 1 post. 528 00:30:51,240 --> 00:30:54,730 We grabbed the first post, and now we printed out its body. 529 00:30:54,730 --> 00:30:58,010 Great. Fantastic. 530 00:30:58,010 --> 00:31:01,470 Any questions so far? 531 00:31:01,470 --> 00:31:04,100 Nope? Any questions? 532 00:31:04,100 --> 00:31:08,340 Okay, so we just created the post model, very basic, 533 00:31:08,340 --> 00:31:10,930 and we're going to add some functions later on. 534 00:31:10,930 --> 00:31:13,600 We can add validations and filtering. 535 00:31:13,600 --> 00:31:15,650 Validations are one of the things 536 00:31:15,650 --> 00:31:18,150 that frameworks solve for you really, really well, 537 00:31:18,150 --> 00:31:21,310 and I don't think you guys had to do this for your CS50 pset, 538 00:31:21,310 --> 00:31:24,000 but if you do web development for your final project, 539 00:31:24,000 --> 00:31:26,280 you're likely going to want to do some sort of validation, 540 00:31:26,280 --> 00:31:28,290 like not having blank user names, 541 00:31:28,290 --> 00:31:31,950 maybe having a password with at least some length, things like that. 542 00:31:31,950 --> 00:31:34,750 And it's really cumbersome to implement these things by ourselves, 543 00:31:34,750 --> 00:31:37,390 and pretty much every single web framework does it for you 544 00:31:37,390 --> 00:31:41,140 and allows you to do it in a very clean way. 545 00:31:41,140 --> 00:31:44,340 And the model is where you generally express those validation rules, 546 00:31:44,340 --> 00:31:48,790 because it's validating whether a model is valid or not. 547 00:31:48,790 --> 00:31:51,350 >> But for now, we're going to put that until later, 548 00:31:51,350 --> 00:31:53,520 and for now we're going to work on another part, 549 00:31:53,520 --> 00:31:55,400 and we're going to try and make a new view 550 00:31:55,400 --> 00:31:59,580 that lists all the posts. 551 00:31:59,580 --> 00:32:02,490 The steps involved in making a new action for listing all the posts 552 00:32:02,490 --> 00:32:04,810 is to grab a list of all the posts 553 00:32:04,810 --> 00:32:11,990 and then render the list of all the posts through a view. 554 00:32:11,990 --> 00:32:16,420 Right here, fortunately enough, we already grabbed all the posts 555 00:32:16,420 --> 00:32:20,310 using this first line, the find all function, 556 00:32:20,310 --> 00:32:22,520 and now what we're going to do is so far 557 00:32:22,520 --> 00:32:25,350 I've been directly setting the body of the response 558 00:32:25,350 --> 00:32:29,090 by passing the string, but now I want to use a view, 559 00:32:29,090 --> 00:32:31,870 and the difference between a view and just doing this 560 00:32:31,870 --> 00:32:35,330 is with a view I can have a nice, big HTML template, 561 00:32:35,330 --> 00:32:37,710 and what I can do is pass it certain variables 562 00:32:37,710 --> 00:32:42,200 and then have the view automatically populate its template 563 00:32:42,200 --> 00:32:44,690 using those variables. 564 00:32:44,690 --> 00:32:50,780 What I'll do is I'll create a new view, 565 00:32:50,780 --> 00:32:55,940 and I'll name the view something like "blog/index," 566 00:32:55,940 --> 00:33:08,480 and I'm going to basically bind this--oh, what am I writing? 567 00:33:08,480 --> 00:33:12,910 My brain is somewhere else. 568 00:33:12,910 --> 00:33:16,600 I'm going to bind the posts variable to the view, 569 00:33:16,600 --> 00:33:19,950 so that way the view has access to this post variable. 570 00:33:19,950 --> 00:33:26,140 >> And so now I need to create this view, 571 00:33:26,140 --> 00:33:28,500 so here we have this folder called "Views," 572 00:33:28,500 --> 00:33:32,150 and first, I'm going to create a new folder under that called "Blog." 573 00:33:32,150 --> 00:33:35,810 This is nice. That way we can have a nice hierarchy for our views. 574 00:33:35,810 --> 00:33:43,910 And then I'm going to create another file in there called "index.php." 575 00:33:43,910 --> 00:33:45,780 Awesome. 576 00:33:45,780 --> 00:33:52,930 Actually, let's have them both here. 577 00:33:52,930 --> 00:33:56,760 Making a view file is probably the simplest part of all this, 578 00:33:56,760 --> 00:33:59,090 and these are probably things you're already familiar with. 579 00:33:59,090 --> 00:34:01,240 We're going to do something really simple, 580 00:34:01,240 --> 00:34:05,360 start saying, "My list of blog posts." 581 00:34:05,360 --> 00:34:14,860 Then we can go through, 582 00:34:14,860 --> 00:34:17,920 and we can iterate through the posts array, 583 00:34:17,920 --> 00:34:21,760 grab every single post and say something like-- 584 00:34:21,760 --> 00:34:25,290 maybe add a line 585 00:34:25,290 --> 00:34:42,460 and then print out the author and the body. 586 00:34:42,460 --> 00:34:44,480 That make sense so far? 587 00:34:44,480 --> 00:34:50,870 And let's see if it works. 588 00:34:50,870 --> 00:34:53,489 Nothing happened. 589 00:34:53,489 --> 00:34:55,090 I wonder why. 590 00:34:55,090 --> 00:34:58,760 Oh, I missed 1 step. Very silly of me. 591 00:34:58,760 --> 00:35:01,640 I created a view, but I didn't set the view as the response, 592 00:35:01,640 --> 00:35:03,190 so you have to do 1 more thing. 593 00:35:03,190 --> 00:35:12,610 You have to do "this response body" and set it to be the view. 594 00:35:12,610 --> 00:35:14,760 There we go. 595 00:35:14,760 --> 00:35:17,200 We have our heading, and then we have a post, 596 00:35:17,200 --> 00:35:20,500 and just for kicks, let's insert another post 597 00:35:20,500 --> 00:35:23,390 so we can see a list. 598 00:35:31,800 --> 00:35:36,650 And insert these 2 posts, 599 00:35:36,650 --> 00:35:39,500 and now if I refresh the page, 600 00:35:39,500 --> 00:35:42,060 we see all these posts here. 601 00:35:42,060 --> 00:35:44,250 >> Does that make sense so far? 602 00:35:44,250 --> 00:35:46,400 Yeah, a question? Oh, okay. 603 00:35:46,400 --> 00:35:51,440 As you can see, we've been able to separate all these codes out 604 00:35:51,440 --> 00:35:53,920 into different sections, and then you can see it's most clear with the view code. 605 00:35:53,920 --> 00:35:57,810 This file here that represents the view, 606 00:35:57,810 --> 00:36:01,220 it only cares about representing data, displaying data. 607 00:36:01,220 --> 00:36:04,310 It gets passed some sort of data, and all it does is just show it to you. 608 00:36:04,310 --> 00:36:07,660 In all other parts of your code, you won't have to worry about any of that, 609 00:36:07,660 --> 00:36:10,480 and similarly, your view code doesn't have to worry anything about 610 00:36:10,480 --> 00:36:13,390 how to access the database and so on and so forth, 611 00:36:13,390 --> 00:36:19,950 which is really good and makes your code a lot more maintainable. 612 00:36:19,950 --> 00:36:23,390 Like I said, views, they're dynamic in that 613 00:36:23,390 --> 00:36:27,080 it's 1 file, but it would generate different views 614 00:36:27,080 --> 00:36:29,940 based on the variables you actually pass in, 615 00:36:29,940 --> 00:36:32,370 and furthermore, there are a lot of different helper functions 616 00:36:32,370 --> 00:36:34,230 that you can use to help you write your code faster, 617 00:36:34,230 --> 00:36:36,320 which I'll show you in just a second. 618 00:36:36,320 --> 00:36:38,050 Yeah. 619 00:36:38,050 --> 00:36:42,490 >> [Student] So $0 is a controller, right? 620 00:36:42,490 --> 00:36:44,000 That second thing. 621 00:36:44,000 --> 00:36:46,090 The question is is $0 a controller? 622 00:36:46,090 --> 00:36:48,610 $0 is a variable I created right here. 623 00:36:48,610 --> 00:36:51,320 I created a view first. I assigned it to some variable. 624 00:36:51,320 --> 00:36:54,960 Then I passed it into this function, set it as the body of the response. 625 00:36:54,960 --> 00:36:57,260 Does that make sense? 626 00:36:57,260 --> 00:37:02,200 [Student] So is view :: factory, is view like a class 627 00:37:02,200 --> 00:37:06,610 or a library [inaudible] factory function? 628 00:37:06,610 --> 00:37:10,640 The question is about the view :: factory function, 629 00:37:10,640 --> 00:37:14,020 and basically this is some more object-oriented programming essentially. 630 00:37:14,020 --> 00:37:18,000 View is the view class, and it has a method called "Factory," 631 00:37:18,000 --> 00:37:24,170 and that's a way to grab the object that's named "blog/index." 632 00:37:24,170 --> 00:37:27,140 And that's some more object-oriented programming stuff 633 00:37:27,140 --> 00:37:33,010 that I'm not going to go into here too much. 634 00:37:33,010 --> 00:37:36,400 Now obviously, we want to create new posts, 635 00:37:36,400 --> 00:37:38,790 but we don't want to have to do it through a database, 636 00:37:38,790 --> 00:37:41,280 so we're going to create a new action for creating a new post, 637 00:37:41,280 --> 00:37:43,050 and there is a lot of stuff we have to do. 638 00:37:43,050 --> 00:37:45,910 >> The first thing we're going to do--let's tackle these things one by one. 639 00:37:45,910 --> 00:37:48,320 The first thing we'll do is we've got to create a form 640 00:37:48,320 --> 00:37:54,460 for inserting a new post, 641 00:37:54,460 --> 00:37:57,360 but I'm also going to add a new action first, 642 00:37:57,360 --> 00:38:01,050 so adding a new action is just as easy as 643 00:38:01,050 --> 00:38:03,490 adding a new function with your controller, 644 00:38:03,490 --> 00:38:13,710 and for now I'm going to do something very basic, 645 00:38:13,710 --> 00:38:20,850 just grab this view and post it, just display it for you. 646 00:38:20,850 --> 00:38:26,220 And then now I'm going to create a new view file, 647 00:38:26,220 --> 00:38:33,690 and I'm going to start writing some stuff. 648 00:38:33,690 --> 00:38:36,540 What's nice about Kohana is that they provide a lot of different helper functions 649 00:38:36,540 --> 00:38:38,790 for you to write view code more easily, 650 00:38:38,790 --> 00:38:41,970 and 1 of those helper functions or helper modules 651 00:38:41,970 --> 00:38:45,860 is around writing forms. 652 00:38:45,860 --> 00:38:49,460 For writing forms, I don't really have to directly write any HTML myself. 653 00:38:49,460 --> 00:38:51,100 You guys have written HTML forms. 654 00:38:51,100 --> 00:38:54,850 You know how it can be really, really painful and cumbersome to write forms. 655 00:38:54,850 --> 00:38:59,970 It's not fun, so fortunately, 656 00:38:59,970 --> 00:39:04,860 we can basically write a form using Kohana's 657 00:39:04,860 --> 00:39:11,190 form helper functions to do it for us. 658 00:39:11,190 --> 00:39:17,340 We're going to basically have fields for every single thing we have, 659 00:39:17,340 --> 00:39:23,160 so one for authors and one for the bodies. 660 00:39:23,160 --> 00:39:27,090 We're going to have a label, and we're going to have an input. 661 00:39:37,450 --> 00:39:41,360 >> And then finally, we're going to have a submission. 662 00:39:49,350 --> 00:39:52,230 And as you can see, this is much cleaner to write 663 00:39:52,230 --> 00:39:58,150 than all that messy HTML, which is kind of nice. 664 00:39:58,150 --> 00:40:00,930 Granted, there are other web frameworks that have it even cleaner than that, 665 00:40:00,930 --> 00:40:04,440 but at least this is better than writing the HTML yourself. 666 00:40:09,400 --> 00:40:11,130 Awesome, so this is what you see. 667 00:40:11,130 --> 00:40:13,530 That's kind of messy, 668 00:40:13,530 --> 00:40:19,720 so I'm going to add a line break there 669 00:40:19,720 --> 00:40:21,180 to make that look a little nicer. 670 00:40:21,180 --> 00:40:23,330 Well, of course, it still looks really, really bad, but we're just focused 671 00:40:23,330 --> 00:40:26,050 on the functionality for now and not on the aesthetics. 672 00:40:26,050 --> 00:40:28,010 No time to do everything. 673 00:40:28,010 --> 00:40:30,600 And as you can see, now we have a super basic form, 674 00:40:30,600 --> 00:40:32,080 which is kind of nice. 675 00:40:32,080 --> 00:40:36,730 This code I would say is cleaner than trying to write an HTML form yourself, 676 00:40:36,730 --> 00:40:40,290 so that's nice. 677 00:40:40,290 --> 00:40:42,030 What's next? 678 00:40:42,030 --> 00:40:49,260 Now we need to do things with the action. 679 00:40:49,260 --> 00:40:51,240 Normally when you write HTML forms, 680 00:40:51,240 --> 00:40:54,070 you have to tell it where it's going to submit the form to. 681 00:40:54,070 --> 00:40:56,050 By default in most web frameworks, 682 00:40:56,050 --> 00:40:58,200 it submits to the exact same URL, so the thing is, 683 00:40:58,200 --> 00:41:01,310 if you send a get request to /blog/new, 684 00:41:01,310 --> 00:41:03,240 it should display you the form, 685 00:41:03,240 --> 00:41:06,810 but if you send a post request to /blog/new with the data, 686 00:41:06,810 --> 00:41:10,000 it should actually try to save that post 687 00:41:10,000 --> 00:41:13,300 and do something with it. 688 00:41:20,630 --> 00:41:22,180 >> What we're going to do is 689 00:41:22,180 --> 00:41:25,320 basically all we have to do to check whether it's a post request or a get request 690 00:41:25,320 --> 00:41:29,350 is to check what are the post variables you can set. 691 00:41:29,350 --> 00:41:34,560 And if the post variable is set, then we're going to try and create a new post. 692 00:41:34,560 --> 00:41:38,440 Again, we just do this, 693 00:41:38,440 --> 00:41:41,090 and that creates a new post, and we're literally going to 694 00:41:41,090 --> 00:41:51,150 set its fields like this, 695 00:41:51,150 --> 00:41:55,640 and then we're going to save it. 696 00:41:55,640 --> 00:41:59,200 And then I'm going to redirect 697 00:41:59,200 --> 00:42:07,660 to the index page so they can see our list of posts again. 698 00:42:07,660 --> 00:42:09,620 Let's try that. 699 00:42:09,620 --> 00:42:15,160 I'll say, "Brandon," 700 00:42:15,160 --> 00:42:18,140 and then submit the post, and if all goes well, 701 00:42:18,140 --> 00:42:21,390 as you can see, it redirected me to the index page, 702 00:42:21,390 --> 00:42:24,140 and if I scroll to the bottom, we have a newly inserted post. 703 00:42:24,140 --> 00:42:26,430 Yay! 704 00:42:26,430 --> 00:42:28,430 Yeah, question. 705 00:42:28,430 --> 00:42:31,760 >> [Student] What if you had entered the exact same thing 706 00:42:31,760 --> 00:42:33,380 you entered before? 707 00:42:33,380 --> 00:42:36,950 Does it check to make sure you haven't duplicated 708 00:42:36,950 --> 00:42:38,810 the same submission? 709 00:42:38,810 --> 00:42:41,660 Be default, no, because by default-- 710 00:42:41,660 --> 00:42:46,470 sorry, the question is if you enter in the exact same data in the form 711 00:42:46,470 --> 00:42:50,180 and submit that, will it allow you to insert a duplicate object, 712 00:42:50,180 --> 00:42:52,550 a duplicate entry, essentially? 713 00:42:52,550 --> 00:42:54,070 Right now, yes, it will allow you to do that, 714 00:42:54,070 --> 00:42:58,860 because in databases it's perfectly valid to have completely duplicate rows, 715 00:42:58,860 --> 00:43:02,260 but if that is a concern, then you can add validations, for example, 716 00:43:02,260 --> 00:43:06,430 to make sure that if this is exactly the same as something that already exists, 717 00:43:06,430 --> 00:43:08,720 then say that it's an invalid object, 718 00:43:08,720 --> 00:43:11,200 and then you can even specify your error message 719 00:43:11,200 --> 00:43:14,390 and say, "Invalid because this already exists" or something like that. 720 00:43:14,390 --> 00:43:22,420 But in this case, I could just create something duplicate. 721 00:43:22,420 --> 00:43:26,010 Now let's try and add some validations. 722 00:43:26,010 --> 00:43:30,400 The problem with this right now is that 723 00:43:30,400 --> 00:43:34,220 I could literally submit a completely blank post. 724 00:43:34,220 --> 00:43:37,500 I can click this button right now, and there we go. 725 00:43:37,500 --> 00:43:40,290 You can't really see it, but this extra line here 726 00:43:40,290 --> 00:43:43,830 indicates that I literally have a new post. 727 00:43:43,830 --> 00:43:46,050 It just has a blank author and a blank body, 728 00:43:46,050 --> 00:43:48,630 and we don't want to allow people to do that. 729 00:43:48,630 --> 00:43:52,550 This is where validation comes in. 730 00:43:52,550 --> 00:43:57,540 >> I can go to my model object, 731 00:43:57,540 --> 00:43:59,530 and now I can add a new function that specifies 732 00:43:59,530 --> 00:44:02,000 what validation rules I should add to this model 733 00:44:02,000 --> 00:44:06,840 to make sure that it is valid or to specify what does it mean to be a valid post? 734 00:44:06,840 --> 00:44:10,210 And I want to say it's only a valid post if both the author and body 735 00:44:10,210 --> 00:44:15,150 are not blank, and this is how you do it in Kohana. 736 00:44:15,150 --> 00:44:18,750 You create a new function called "Rules," 737 00:44:18,750 --> 00:44:20,210 and then you basically return an associative array 738 00:44:20,210 --> 00:44:24,230 that defines the validation rules for this object. 739 00:44:24,230 --> 00:44:27,530 We're going to return the array, and then what we're going to do is 740 00:44:27,530 --> 00:44:32,820 say "author," it goes to an array, 741 00:44:32,820 --> 00:44:37,720 which goes to another array called "not empty." 742 00:44:37,720 --> 00:44:41,480 And then I'm going to say "body." 743 00:44:50,980 --> 00:44:54,120 Okay, and the syntax for this and structure for this 744 00:44:54,120 --> 00:44:56,530 may look a little cumbersome and a little complicated. 745 00:44:56,530 --> 00:44:59,330 If you read the documentation, it's pretty straightforward to figure out, 746 00:44:59,330 --> 00:45:02,500 But essentially this is what you need to do to specify 747 00:45:02,500 --> 00:45:04,130 some validation rules, and there are a lot of different rules 748 00:45:04,130 --> 00:45:06,810 that Kohana will give you for free, like you can add rules to say 749 00:45:06,810 --> 00:45:08,410 it must be at least this length. 750 00:45:08,410 --> 00:45:11,800 Maybe it has to be numeric. Maybe it has to be alpha numeric. 751 00:45:11,800 --> 00:45:14,410 Maybe it has to be at most this length, so on and so forth. 752 00:45:14,410 --> 00:45:17,730 There are a lot of different rules that Kohana provides for you, 753 00:45:17,730 --> 00:45:19,610 and you can go on their website, look at the documentation, 754 00:45:19,610 --> 00:45:23,150 and you can see all the different things that you can do. 755 00:45:23,150 --> 00:45:25,650 >> But this is all I have to do, 756 00:45:25,650 --> 00:45:30,490 and now let's see what happens 757 00:45:30,490 --> 00:45:34,060 if I submit a blank post. 758 00:45:34,060 --> 00:45:36,960 What's going to happen? Oh, no, I get an error. 759 00:45:36,960 --> 00:45:39,440 I get a validation exception. 760 00:45:39,440 --> 00:45:41,070 Well, it's good. 761 00:45:41,070 --> 00:45:43,200 It told me that my model is invalid, 762 00:45:43,200 --> 00:45:45,780 but I don't want to display an exception 763 00:45:45,780 --> 00:45:48,720 to my users when they try to submit something invalid, right? 764 00:45:48,720 --> 00:45:51,560 I want to give them some sort of friendlier error message 765 00:45:51,560 --> 00:45:53,610 when something goes wrong. 766 00:45:53,610 --> 00:46:01,830 What we're going to do is we're going to 767 00:46:01,830 --> 00:46:04,490 wrap everything in a try catch loop. 768 00:46:04,490 --> 00:46:06,750 Actually, I think this is also something you have not learned yet 769 00:46:06,750 --> 00:46:10,820 in CS50, because C, the programming language C, 770 00:46:10,820 --> 00:46:14,000 doesn't have exceptions, but almost every single other language 771 00:46:14,000 --> 00:46:16,700 has exceptions, so really, really briefly, 772 00:46:16,700 --> 00:46:19,430 an exception is something that a piece of code can 773 00:46:19,430 --> 00:46:21,430 throw an exception when something goes wrong, 774 00:46:21,430 --> 00:46:23,410 but then maybe some other piece of code higher up 775 00:46:23,410 --> 00:46:25,810 can catch that exception and do something with it. 776 00:46:25,810 --> 00:46:27,710 >> For example, in this case, 777 00:46:27,710 --> 00:46:29,940 the piece of code that's trying to save a model, 778 00:46:29,940 --> 00:46:33,170 it validates the model, and if it says, "Okay, this model is invalid," 779 00:46:33,170 --> 00:46:36,150 it's going to throw an exception, and this is kind of equivalent to 780 00:46:36,150 --> 00:46:39,870 in C you might return a -1 or something like that. 781 00:46:39,870 --> 00:46:42,320 And then for me, this function, my code 782 00:46:42,320 --> 00:46:46,310 at a higher level, I can try and catch that exception 783 00:46:46,310 --> 00:46:49,330 and basically say, "Okay, if I catch the exception, what am I going to do?" 784 00:46:49,330 --> 00:46:51,570 Or I could choose not to catch that exception and let someone higher up 785 00:46:51,570 --> 00:46:54,400 catch the exception, or if nobody catches it, 786 00:46:54,400 --> 00:46:56,820 then the whole program crashes and says, 787 00:46:56,820 --> 00:46:59,170 "Something went wrong, and I couldn't handle it." 788 00:46:59,170 --> 00:47:04,490 >> But what we do is you wrap a piece of code in a try block, 789 00:47:04,490 --> 00:47:09,030 and then you also add something called a catch block, 790 00:47:09,030 --> 00:47:17,300 which is the sort of code that will try and catch exceptions that may occur. 791 00:47:17,300 --> 00:47:20,430 And so if I catch this particular exception 792 00:47:20,430 --> 00:47:23,110 or invalidation exception, then what I'm going to do is 793 00:47:23,110 --> 00:47:31,210 I'm going to set the errors--I think that's how I do it-- 794 00:47:31,210 --> 00:47:35,370 and I'm going to set the errors to some object. 795 00:47:35,370 --> 00:47:40,920 And then what I'm going to do is if it hits this exception, 796 00:47:40,920 --> 00:47:43,090 it's not going to redirect, and if it doesn't redirect, 797 00:47:43,090 --> 00:47:46,160 it's going to come out of the if blog 798 00:47:46,160 --> 00:47:49,920 and hit this blog/new, which is want I want to do. 799 00:47:49,920 --> 00:47:53,190 If there is an error, then I want to go back to the form 800 00:47:53,190 --> 00:47:55,100 and display those errors. 801 00:47:55,100 --> 00:48:00,780 Now what I want to do is I want to pass in those errors 802 00:48:00,780 --> 00:48:07,010 to the view. 803 00:48:07,010 --> 00:48:10,360 Okay, I think I have the view right here, 804 00:48:10,360 --> 00:48:14,660 and basically I want to display those errors if they exist. 805 00:48:14,660 --> 00:48:19,740 Before I write the HTML for that, I'm going to really quickly 806 00:48:19,740 --> 00:48:21,720 show you what the structure of this errors variable looks like, 807 00:48:21,720 --> 00:48:23,080 and this is a good practice in general. 808 00:48:23,080 --> 00:48:25,070 A lot of times you get something back from some method, 809 00:48:25,070 --> 00:48:27,250 some function in the web framework, 810 00:48:27,250 --> 00:48:29,410 and you don't know what the variable looks like, 811 00:48:29,410 --> 00:48:31,210 so you don't know how to work with it. 812 00:48:31,210 --> 00:48:37,790 I'm going to use a print r method to basically print it out. 813 00:48:37,790 --> 00:48:41,100 >> And as you can see, it tells me it's an associate array, 814 00:48:41,100 --> 00:48:44,880 and you have a key, author, points to this string, 815 00:48:44,880 --> 00:48:47,050 author must not be empty, and another key, body, 816 00:48:47,050 --> 00:48:49,680 points to another string, body must not be empty. 817 00:48:49,680 --> 00:48:52,130 I'm like, okay, cool. 818 00:48:52,130 --> 00:48:56,230 Then I can iterate through the array and print out every single message. 819 00:48:56,230 --> 00:49:02,150 It's basically like an associative array with a bunch of messages. 820 00:49:02,150 --> 00:49:13,500 What I'm going to do is "if errors," 821 00:49:13,500 --> 00:49:17,140 and I'm going to create an unordered list, 822 00:49:17,140 --> 00:49:20,860 and I'm going to iterate through all the errors. 823 00:49:33,730 --> 00:49:38,710 And this, and now I'm going to try submitting this again, 824 00:49:38,710 --> 00:49:41,200 and let's see what we get. 825 00:49:41,200 --> 00:49:45,370 Now we get this nice list of errors, 826 00:49:45,370 --> 00:49:48,630 and this is still pretty ugly, but this obviously can be formatted to look nice, 827 00:49:48,630 --> 00:49:51,620 but the basic idea is just in a few lines of code, 828 00:49:51,620 --> 00:49:53,720 we were able to validate our model, 829 00:49:53,720 --> 00:49:56,510 make sure that certain fields weren't empty, 830 00:49:56,510 --> 00:49:59,740 and if something went wrong, then return some sort of error message 831 00:49:59,740 --> 00:50:01,760 I could then present back to the user. 832 00:50:01,760 --> 00:50:03,910 You can also customize your validation 833 00:50:03,910 --> 00:50:07,590 so that you can actually have an error message 834 00:50:07,590 --> 00:50:09,620 that is more specific to your application or something like that. 835 00:50:09,620 --> 00:50:14,600 All that is generally customizable. 836 00:50:14,600 --> 00:50:17,150 >> Unfortunately, we're running out of time, 837 00:50:17,150 --> 00:50:20,040 so I'm going to have to cut off the live coding session here. 838 00:50:20,040 --> 00:50:22,980 There are a bunch of other features that I want to demonstrate for you 839 00:50:22,980 --> 00:50:25,650 in this example. 840 00:50:25,650 --> 00:50:27,620 For example, you can add templates to your site, 841 00:50:27,620 --> 00:50:31,110 so maybe there is some sort of HTML code that you want to apply 842 00:50:31,110 --> 00:50:35,190 to every single page in your site, and instead of pasting that 843 00:50:35,190 --> 00:50:40,630 in every single view file you have, which obviously would be a bad practice, 844 00:50:40,630 --> 00:50:43,020 you can basically define these templates, 845 00:50:43,020 --> 00:50:46,660 and then in your controller say, "Okay, I'm using this template." 846 00:50:46,660 --> 00:50:50,130 "Have all my views use this template." 847 00:50:50,130 --> 00:50:52,470 And the one last thing I want to demonstrate to you as well 848 00:50:52,470 --> 00:50:57,800 that we don't have time for is cross-site scripting, 849 00:50:57,800 --> 00:51:01,430 and basically I think you guys have probably seen in CS50-- 850 00:51:01,430 --> 00:51:03,770 I think David Malan probably talked about how you can usually inject 851 00:51:03,770 --> 00:51:08,040 JavaScript code into--have you talked about this? 852 00:51:08,040 --> 00:51:10,220 Maybe? Maybe not? 853 00:51:10,220 --> 00:51:12,670 >> But a lot of times you can inject malicious JavaScript code 854 00:51:12,670 --> 00:51:15,630 into someone's database, and if they don't escape that properly, 855 00:51:15,630 --> 00:51:18,280 then when they present that data back to the user, 856 00:51:18,280 --> 00:51:21,310 then it may run some sort of random JavaScript code you don't want to happen, 857 00:51:21,310 --> 00:51:23,050 and I was going to demonstrate how you'd do that within Kohana. 858 00:51:23,050 --> 00:51:25,380 It's actually really, really easy. 859 00:51:25,380 --> 00:51:31,920 I could do it right now in 2 seconds literally. 860 00:51:31,920 --> 00:51:34,560 All you have to do is basically wrap these things 861 00:51:34,560 --> 00:51:46,920 in this thing called HTML entities. 862 00:51:46,920 --> 00:51:51,180 And that will automatically escape all the characters properly 863 00:51:51,180 --> 00:51:54,730 and make sure you don't get this problem. 864 00:51:54,730 --> 00:51:57,220 [Student] You spelled the first test incorrectly. 865 00:51:57,220 --> 00:52:01,030 [Brandon Liu] Oh, oops. 866 00:52:01,030 --> 00:52:06,420 >> Okay, that's all I had to share with you for today. 867 00:52:06,420 --> 00:52:09,930 These slides are going to be posted, but these are generally 868 00:52:09,930 --> 00:52:13,900 the only resources you should really need to get started with Kohana. 869 00:52:13,900 --> 00:52:16,770 You can go to the website. They have a user guide, and they also have an API explorer. 870 00:52:16,770 --> 00:52:20,630 We can explore all the different functions and helper functions they have for you. 871 00:52:20,630 --> 00:52:22,070 They generally have enough information on the website 872 00:52:22,070 --> 00:52:25,310 that you can use to get started and get going with Kohana. 873 00:52:25,310 --> 00:52:30,840 There aren't that many tutorials, I think, for Kohana, outside of 874 00:52:30,840 --> 00:52:33,880 what they have on the website here, so this is probably your best bet. 875 00:52:33,880 --> 00:52:35,600 But if you want to go with the web framework 876 00:52:35,600 --> 00:52:37,120 and you don't want to have to pick up a new language, 877 00:52:37,120 --> 00:52:39,780 and you want something that is relatively lightweight and has an easy learning curve, 878 00:52:39,780 --> 00:52:41,570 I would definitely suggest Kohana. 879 00:52:41,570 --> 00:52:44,040 That's probably the best offering for that. 880 00:52:44,040 --> 00:52:46,830 The funny thing, though, is if we were using Ruby on Rails, 881 00:52:46,830 --> 00:52:48,550 we could have replicated what we just did 882 00:52:48,550 --> 00:52:51,430 and probably more in under 3 minutes. 883 00:52:51,430 --> 00:52:54,710 No joke, but learning Ruby on Rails takes a lot longer 884 00:52:54,710 --> 00:52:56,780 than it would take to learn Kohana. 885 00:52:56,780 --> 00:52:58,840 It's basically your choice on what you want to choose to learn, 886 00:52:58,840 --> 00:53:01,260 but if you want to get up and running quickly, 887 00:53:01,260 --> 00:53:03,870 Kohana is definitely a very good choice. 888 00:53:03,870 --> 00:53:06,730 >> Any last questions before we end? Yes. 889 00:53:06,730 --> 00:53:08,020 [Student] How would we integrate that 890 00:53:08,020 --> 00:53:13,120 in a CSS framework like you were using when you were instructing? 891 00:53:13,120 --> 00:53:16,700 The question is how would we integrate that with a CSS framework? 892 00:53:16,700 --> 00:53:18,740 What we would probably do is we would probably include 893 00:53:18,740 --> 00:53:21,090 a new folder where we would dump all our CSS files, 894 00:53:21,090 --> 00:53:23,010 and then we'd also add a new template. 895 00:53:23,010 --> 00:53:26,090 In the template we'd include those CSS files 896 00:53:26,090 --> 00:53:28,410 to make sure they're referenced on every single page, 897 00:53:28,410 --> 00:53:32,220 and then when you actually are writing HTML, 898 00:53:32,220 --> 00:53:34,010 you just add appropriate classes and whatnot, 899 00:53:34,010 --> 00:53:36,100 and for example, when you're using something like the form 900 00:53:36,100 --> 00:53:40,710 helper function, you can add more parameters afterwards 901 00:53:40,710 --> 00:53:42,830 to specify what classes you want to be attached to various things 902 00:53:42,830 --> 00:53:47,820 so they could style it properly, and that's basically how you would go. 903 00:53:47,820 --> 00:53:50,100 >> Any other questions? 904 00:53:50,100 --> 00:53:52,090 Awesome. 905 00:53:52,090 --> 00:53:58,540 Thank you for your time, and thank you for coming. 906 00:53:58,540 --> 00:54:05,170 I wasn't going to add very much else, 907 00:54:05,170 --> 00:54:08,560 but 1 really quick thing is we don't have a link 908 00:54:08,560 --> 00:54:12,590 to the form. 909 00:54:12,590 --> 00:54:14,310 Really dumb. 910 00:54:14,310 --> 00:54:20,110 Let's add a--actually in the view, blog, index, 911 00:54:20,110 --> 00:54:23,890 let's really quickly add a link that goes to the new page, 912 00:54:23,890 --> 00:54:26,770 the page where we can insert a new post. 913 00:54:26,770 --> 00:54:29,950 We're going to do this. 914 00:54:29,950 --> 00:54:34,020 What's nice is there is this whole group of HTML helper functions 915 00:54:34,020 --> 00:54:37,090 which do different things for you, so you already saw the entities 916 00:54:37,090 --> 00:54:41,980 function here, but they also have a function called "anchor," 917 00:54:41,980 --> 00:54:45,400 which you can type in blog/new 918 00:54:45,400 --> 00:54:49,550 and say, "Post a new blog." 919 00:54:49,550 --> 00:54:51,850 And it would create that link for you, 920 00:54:51,850 --> 00:54:54,120 and this seems really trivial to do, but this is nice, 921 00:54:54,120 --> 00:54:58,720 because suppose that you are moving your website from 1 domain to another. 922 00:54:58,720 --> 00:55:01,390 And if you just wrote out the URLs yourself, 923 00:55:01,390 --> 00:55:04,350 then you would have to change all the URLs. 924 00:55:04,350 --> 00:55:06,850 Or maybe you moved it from 1 subfolder to another subfolder. 925 00:55:06,850 --> 00:55:08,790 You would have to change all those URLs yourself, 926 00:55:08,790 --> 00:55:12,180 and that's no fun. 927 00:55:12,180 --> 00:55:14,510 You can use this anchor right here, 928 00:55:14,510 --> 00:55:18,950 and you can change the domain or the subfolder prefix 929 00:55:18,950 --> 00:55:20,640 in the configuration file once, 930 00:55:20,640 --> 00:55:22,980 and then it will apply that everywhere, and this is, again, 931 00:55:22,980 --> 00:55:26,930 a great example of do not repeat yourself, DRYing your code out. 932 00:55:26,930 --> 00:55:30,370 Wherever you're repeating yourself, try and extract in some sort of configuration file 933 00:55:30,370 --> 00:55:34,160 or to a different function and have it handle that for you. 934 00:55:34,160 --> 00:55:42,930 >> And the very last thing that I wanted to show you was 935 00:55:42,930 --> 00:55:50,250 suppose we're back at this post, and I had composed some really long essay, 936 00:55:50,250 --> 00:55:52,670 but I forgot to include my author. 937 00:55:52,670 --> 00:55:55,210 Now when I click "Submit Post," 938 00:55:55,210 --> 00:55:57,270 I just lost everything. 939 00:55:57,270 --> 00:56:00,000 No! Really sad. 940 00:56:00,000 --> 00:56:03,870 So how do you deal with that? 941 00:56:03,870 --> 00:56:06,070 This is what we do. 942 00:56:06,070 --> 00:56:09,340 What we do is here for these input and text area functions, 943 00:56:09,340 --> 00:56:12,700 if we include a second parameter, then the value of that second parameter 944 00:56:12,700 --> 00:56:16,620 is going to be what the field is going to be initially populated with. 945 00:56:16,620 --> 00:56:23,570 What we could do is in our blog controller, 946 00:56:23,570 --> 00:56:25,360 we could bind another variable. 947 00:56:25,360 --> 00:56:27,050 Call it "values" maybe. 948 00:56:27,050 --> 00:56:30,620 And pass in the post array, literally. 949 00:56:30,620 --> 00:56:32,620 That means that if the validation failed, 950 00:56:32,620 --> 00:56:36,570 pass to me the post array that I submitted from the last request, 951 00:56:36,570 --> 00:56:38,420 and that way I can use the values from my last submission 952 00:56:38,420 --> 00:56:44,540 to repopulate the fields. 953 00:56:44,540 --> 00:56:49,600 Now I can do something like values author 954 00:56:49,600 --> 00:56:55,180 and values body, and that way now if I do some random stuff 955 00:56:55,180 --> 00:57:01,490 and click "Submit Post," then it stays there. 956 00:57:01,490 --> 00:57:03,830 But we're going to run into another problem. 957 00:57:03,830 --> 00:57:07,670 That works, but if I go to the page the very first time, 958 00:57:07,670 --> 00:57:09,720 it's going to crash, and that's because the very first time 959 00:57:09,720 --> 00:57:13,730 we go to the page, this post variable has not been defined yet. 960 00:57:13,730 --> 00:57:18,170 It's null. It doesn't exist. 961 00:57:18,170 --> 00:57:21,630 >> And what we want to say is if this key exists, 962 00:57:21,630 --> 00:57:27,750 then return the value of this array, 963 00:57:27,750 --> 00:57:30,450 but if the key doesn't exist, then return a blank string. 964 00:57:30,450 --> 00:57:32,150 That's the functionality we want here. 965 00:57:32,150 --> 00:57:34,690 We want to check if the key exists before trying to access the array, 966 00:57:34,690 --> 00:57:36,580 and fortunately enough, 967 00:57:36,580 --> 00:57:38,570 Kohana also gives us a helper function for that. 968 00:57:38,570 --> 00:57:41,040 They have this whole suite of functions 969 00:57:41,040 --> 00:57:43,660 under the name ARR, short for array, 970 00:57:43,660 --> 00:57:45,800 and they have 1 function called "get," 971 00:57:45,800 --> 00:57:48,690 and you can pass in the array, 972 00:57:48,690 --> 00:57:50,740 and you can pass in the name of the key. 973 00:57:50,740 --> 00:57:54,330 Then basically what it will do is it will try to get that key, 974 00:57:54,330 --> 00:57:56,470 but if that key doesn't exist in the array, 975 00:57:56,470 --> 00:58:00,900 then it will return blank, or we can also specify a default, I believe, 976 00:58:00,900 --> 00:58:03,500 which is nice. 977 00:58:09,740 --> 00:58:13,150 Now if we do the same thing again, 978 00:58:13,150 --> 00:58:15,970 then you see now it works the first time around, 979 00:58:15,970 --> 00:58:18,080 and again, if we type in some random stuff 980 00:58:18,080 --> 00:58:23,210 and try and submit, then it stays there. 981 00:58:23,210 --> 00:58:31,640 >> And I guess I can also show you how to add a template really quickly. 982 00:58:31,640 --> 00:58:36,140 What we can do first is we can add a new view called "template.php" 983 00:58:36,140 --> 00:58:38,890 within the Views folder, 984 00:58:38,890 --> 00:58:44,730 and what I'm going to do is I'm going to print out something called "content," 985 00:58:44,730 --> 00:58:49,130 which is going to be my main content. 986 00:58:49,130 --> 00:58:51,380 And maybe at the very bottom I'm going to add, say, 987 00:58:51,380 --> 00:58:53,340 copyright. 988 00:58:53,340 --> 00:58:56,150 [inaudible student question] 989 00:58:56,150 --> 00:58:58,050 [Brandon Liu] Maybe this is a super basic template I want to use. 990 00:58:58,050 --> 00:59:02,840 I want to have a folder with my copyright on every single page, 991 00:59:02,840 --> 00:59:05,560 and now what I'm going to do within my controller 992 00:59:05,560 --> 00:59:07,740 is now instead of saying, "extends Controller" 993 00:59:07,740 --> 00:59:11,870 I'm going to say, "extends Controller_Template," 994 00:59:11,870 --> 00:59:15,890 and now instead of saying, "response body is equal to this view," 995 00:59:15,890 --> 00:59:24,110 I'm going to say, "this template content is --" 996 00:59:24,110 --> 00:59:27,690 and I think--do I put an equal sign? 997 00:59:27,690 --> 00:59:32,710 I forget. Yeah, I thought so. 998 00:59:32,710 --> 00:59:37,710 And now I set that content variable to equal the view. 999 00:59:37,710 --> 00:59:40,960 I can do the same here. 1000 00:59:49,620 --> 00:59:57,170 And now if I refresh, you can see now this copyright is added there, 1001 00:59:57,170 --> 01:00:00,350 and just make some random post, 1002 01:00:00,350 --> 01:00:06,760 and then, again, you should see that the copyright is at the very bottom of the page. 1003 01:00:06,760 --> 01:00:10,730 >> Great. That's all I wanted to show you guys. 1004 01:00:10,730 --> 01:00:14,970 [Applause] 1005 01:00:14,970 --> 01:00:18,950 Any questions? 1006 01:00:18,950 --> 01:00:21,000 [CS50.TV]