>> Alright.
So the courses of instruction.
Harvard's course catalog has several thousands of courses,
there's several hundreds of faculty
and there's several dozens of fields,
computer science just being one of them,
and I thought we'd use this as an opportunity to start thinking
about how to implements a standard web based application
using PHP and Laravel and also how we might model some
of these real world entities.
So to do this we'll use Laravel, which is just a framework
that adds a lot of features to PHP,
much like Jango adds the same to Python,
Rails adds the same to Ruby.
So there's a couple of different ways
in which you can install Laravel.
The documentation for Laravel, as you may recall
from last week, is actually pretty rich
and you can certainly on your own at home play
around with this and follow the instructions specifically,
but I'm going to go ahead and just give us a quick,
whirlwind tour of some of them,
similar to what Tim did the other day,
and I'm first going to use this.
So Composer is sort of a new and improved feature
for the PHP world that makes it easier to install things.
If you're familiar with Act Get in the [inaudible] world
or [inaudible] world or Yum in the [inaudible]
and red hat world, this is yet just another tool
that allows you to install things
for this particular environment.
So if I do Composer, Create Project and so forth,
which I'll do here, let's go ahead and just, I'll walk you
through some of these steps as we did last time
without the percent sign, dammit, without Composer.
So with Composer-I'm using a different virtual machine
than I did before.
Let's do this real fast.
Alright, so installing Composer is as simple as this.
So I'll make a, I'll get up on my soapbox for a while too.
This is actually a very increasingly common paradigm
for installing things on people's computers these days,
where you essentially retrieve an arbitrary URL,
pipe it through an interpreter and then just do what
that arbitrary chunk of code you just downloaded says to do.
It's very easy as you're about to see but realize the skeptic
in your should think that this is among the more insecure
things you could ever do; downloading an arbitrary file
from the internet and then blindly running it by way
of the pipe, but we will trust that the installer is
in fact legitimate today.
I'm going to go ahead and run this and this is going
to download the program.
It's going to download a file called composer.farphparchive,
and I'm going to go ahead and do this,
sudo-first actually chmod755composer.far,
which is going to make it executable by everyone,
and then I'm going to do sudo mv composer
to user local bin composer, and what is user local bin
for those familiar with Linux?
To be clear I'm doing this inside
of the appliance even though I'm using terminal on my MAC.
[Low audio] Exactly, so in a directory,
actually a slight clarification.
In the directory typically called /bin
or /userbin are indeed some very popular programs
like CD and LS and so forth.
In user local bin by convention is nothing by default and we,
the users of the system, can put anything we want in there
and by default it's in your so called search path so that
when you just type composer the operating system, Linux,
is going to look in that directory among others
to find the program in question, and it turns out that it's fine
to rename it with the file extension
because we've made it executable.
This thing will actually run as we intend.
Okay so now I'm going to go back
to the line I proposed we execute earlier, which is this;
composer and then it takes a command line argument,
which I know only from reading the documentation,
called create-project.
I need to specify the name of the project on which I want
to base mine and Laravel/Laravel essentially refers
to a get hub repository in which that code lives.
Catalog is an arbitrary name I'm going to give to my project,
the course catalog, but you could call it foo
or anything else, and prefer-dist.
It's a little cut off.
It just means prefer the latest distribution.
So I'm going to go ahead and hit Enter and as Tim said
on Wednesday it can sometimes take a while,
especially if you're on a slow connection
or if you haven't cached some of these things,
but what this is doing is it's checking
out a whole bunch of code from online.
It's slowly going to download it to my computer
and when it's finally done,
I'm going to have a directory called Catalog inside
of which is-oops, that's why.
Catalog, let's do that again.
Okay don't run that one.
Apparently you have an existing Catalog directory and it notices
that at the last minute.
This is going to have for me a directory called Catalog inside
of which is all the code I need
to start writing a web based application with Laravel.
So within a few seconds this should kick in
and once we have this-and again if you're not at the point
where you're able to follow along right now,
it's totally fine, it's pretty easy
to recreate these steps afterward in your own appliance.
Alright. Alright in the meantime, as we do this,
I'm going to pull up one other hint.
So you might have seen Tim mention
or edit this file last time.
What is an etc/host file and where does it live?
Alright let's roll back in time.
What is DNS, which we discussed not too long ago?
Alright, so domain name system.
This is just the infrastructure on the internet
that does what in a sentence?
Yeah. [Low audio] Exactly.
It converts domain names to IP addresses and vice versa,
so it's effectively a big database table
with these two columns that have fully qualified domain names
on the left and IP address on the right, for instance,
and it just converts the two.
Now that's all fine and good but if you actually want
to create new host names on the internet, new domain names
on the internet, you typically have to give someone money,
you have to configure things on their server, their name servers
and so forth, which is a real pain, and especially
if we don't really want to buy a domain name, we just want
to pretend like we have our own local domain,
for instance called Catalog or Catalog.com
or anything like that.
Well etc/host allows you to hard code mappings between host names
and IP addresses on your own local computer so as
to essentially trick the computer into thinking
that that domain name actually exists at that IP.
It is checked first before the actually DNS system itself
is checked.
So let me go into this terminal window and it looks
like in the background there,
everything had downloaded successfully,
so what I'm now going to do is the following.
If you're more familiar from your 50th or 51 days
with G Edit, I'm going to go ahead and do this with G Edit
at first, but I'm going to go ahead
and do sudo g edit/etc/hosts inside of the appliance,
and when I do this I'm going to get a window
that looks like this one here.
Somewhat cryptic lines only because they're long,
but essentially it's mapping 127.0.0.1
to a few different host names, one of which is local host,
one of which is appliance and then a couple more;
localhost.localdomain, appliance.localdomain.
These are just conventions to use things like localhost
as default host name and localdomain
as a default top level domain or TLD.
So notice what I already did in advance here.
If I want to trick the appliance into thinking
that Catalog is indeed a real website on the internet,
I can tell the appliance that its IP address is 127.0.0.1.
Now why that address?
What is that IP?
Where did it come from?
Who did I have to ask for that?
[Low audio] What's that?
[Low audio] It's the loop back address,
which simply means this is the default IP address given
to most any computer in the world that supports IP,
internet protocol, and it's a loop back address
in the sense that it reflects it.
It refers to itself.
So all of us, if we have an appliance or even a MAC
or PC have a local IP address of 127.0.0.1 and it's only used
so that you can talk to yourself.
I could in theory figure
out what the appliance's IP address is and type in 192.168
or whatever that number is but it's not necessary.
This one's going to be invariant and will simply always work.
So once I save that that ultimately means
that in a browser I can go to http://Catalog, as I'll do here,
but a few things are broken out of the gates.
So what does this typically mean,
when I don't have permissions, forbidden, to do something,
what's your first instinct to solving this problem?
I did download a directory called Catalog
into my V hosts directory, which from last week you'll recall is
where all of our sort of local websites will live.
What might be broken?
How to fix?
[Low audio] Permissions.
Permissions of what?
Okay files potentially.
So let's go back to our terminal window here.
I'm in my V host directory.
Let me do LS and notice there's a few things.
Local host, which comes with the appliance, we put it there.
Catalog.original, which I put there earlier,
that's sort of the finished version of what we're
about to dive into, and Catalog, which I just downloaded.
If I want to look at the permissions
on the Catalog directory, I can do LS-L and notice
that they're a little different from the others.
What permission is apparently the Catalog directory,
which we just downloaded, lacking by default?
Even if you have no idea what the word is you can surly infer
what's different about each of these three lines?
Okay what's missing between line one and line two of the output?
Yeah? [Low audio] Yes, so it's missing the X, which stands
for executable and what does it mean
if a directory is executable?
It just means you can open it.
You can CD into it.
You can access files inside of it.
So if a file's executable, means it's like a program you can run.
A directory is executable if you can go into it.
So I can fix this with a few ways.
I could do chmod A plus X, which means all plus executable
and then I can say Catalog.
Or recall from CS50 or other classes,
I can be a little fancier and say 0711 or more simply 711
on Catalog, hit Enter, nothing seems to happen
but if I do LS-L again, notice that it's executable
by my group, which is the middle three bits and by all,
which is the right three bits, and it suffices for those
to be identical for our purposes.
Alright I feel like we're in pretty good shape.
Let's go back to the browser and let's reload,
dammit, still forbidden.
So what else might be broken?
Where else might we want to look?
So we fixed Catalog, so if I go into Catalog,
let's do another LS, now there's more stuff in here and frankly
at first glance it's overwhelming
and I dare say this is one of the headaches of frameworks
out the gate and that you sort of have a lot of deal with,
but we'll hone in on what's important today
and the most important thing for now is this directory.
If you've used the appliance before you might know
that the publically accessible directory is called Public,
or in years past it was called HTML but we've since changed it
to the more common Public.
So if I do chmod 711 of Public
that would seem to fix that issue.
So now the Public directory is also executable
and therefore accessible.
Let's see if we're one step closer.
Let's reload, still no good and now it's going
to get a little challenging; this one's a little harder.
If I go into Public, recall that Tim showed us this one.
If I do LS-AL, which lists all files in the long format,
there's a dot file so to speak
where a dot file is just a file whose file name begins
with a dot.
What does htaccess do?
Well let's look in that.
Rather than use G Edit, I'm going to use VIM,
you can use EMax or Nano or any of your preferred editors,
but if I look in here this has a bunch of low level stuff
that came with Laravel's distribution.
This is a file that the Apache web server understands
and long story short, these several lines
of configuration trick the whole Public directory
into routing almost all HTTP requests to take a guess.
Which file?
All incoming HTTP requests with a few exceptions would be routed
to what file, otherwise known as a front controller?
>> Index.
>> Index.php.
So even if there's a foo.php file or a bar.php file
in my Public directory, the meaning of these lines
in general is to route all requests
to index.php unless those files do indeed exist because things
like bang-f means if the filename doesn't exist,
and if the directly doesn't exist,
route all requests to index.php.
So in a bit this is going to allow us to have URL's
like http://Catalog/courses/123
where 123 is the unique identifier for a course
but it's clearly not the name of a file so that request is going
to be routed to index.php who's going to figure out,
thanks to Laravel and you, where to route
that incoming HTTP request.
But there's a problem.
Notice by default that htaccess file is not readable
or executable.
Executability is not important but I do want to do chmod644
on dot htaccess or A plus R. Now notice
that if I do an all listing, now it's readable.
Now if I cross my fingers here and reload, whew,
we have it up and running.
So stupid steps that you need to go through at first,
but the fact that I sort of went through it
so pedantically is exactly the kinds
of diagnostic steps you should think
about whenever something's wrong,
and even when a directory might be awry,
realize there might also be dot files,
in particular dot HTAccess which are quite common
in any Apache distribution,
which might create headaches like that one.
So I'm getting a little tired of going back
and forth to the appliance.
I'd much rather work on my MAC or even on my PC.
So how can I do this same etc/host trick?
Well notice that first let me figure
out what the appliance's IP address is.
I can do ifcongig, which shows me some fairly cryptic output
but notice next to eth0 or rather next to eth1,
there's this IP address here.
I'm going to go ahead and copy that and inside
of my own MAC's terminal window I'm going to edit sudo etc/hosts
on my own MAC, which gives me a similar looking file.
All I'm going to do is this.
So I'm going to trick my MAC
into thinking http://Catalog actually maps
to that specific IP address.
Alright, so let's go ahead and save this, now let me close
that terminal window and now on my MAC using any browser I want,
http://Catalog and I see the same thing.
Now I don't even have to bother going back into [inaudible]
which you might not like to do anyway, so now I'm back
on my own computer using really the appliance
as just a web server.
Alright, so here's just a quick recap of some
of the file permissions we might want to change
and I cut some corners in things like the favicon or all
that interesting for now.
Alright, so now let's start
to configure this default Laravel installation
for a database.
So Tim did this briefly last time too and the file
in question is this one here.
So inside of, let me change this to be consistent,
Catalog app config database.php.
So let's go into app, let's go into config
and let's go into database.php.
So this is a pretty long file, most of which is comments,
but you'll see if you read through this closely
that you can configure Laravel and a lot a frameworks
to support different types of databases.
Maybe you're using MySQL, maybe you're using Post [inaudible],
maybe you're using SQL Lite, there's other things
but if we're using MySQL