>> You might recall from last time that we looked
at this final example of MVC,
a model view controller architecture,
and it pretty much boiled
down to organizing things a little more cleanly
in directories.
And if I do tree, which is just a command
that shows me the file system like this, we had a nice
and neat templates directory, a public directory, and so forth.
And the general idea was only put
in the public directory the files that really need
to be public, and everything else should be nicely organized.
But effectively, inside of this public directory we had all
whole bunch of controllers, individual controllers
that somehow required or included other files.
But we really didn't get much more functionality than that,
and, in fact, we kind of had to implement this notion of views,
or templates, ourselves, with the extract function
and a render function that we implemented ourselves.
And as it is we start doing this, we'll start to realize --
we'll start to build up a wish list, like, oh, wow,
I wish I had a library for sending emails.
I wish I had a library for creating a database.
I wish I have library for dot dot dot.
And that's why these libraries exist.
This is why people use Django with Python,
because it just gives you more.
It's why people use Rails with Ruby
because it just gives you more.
That's why we'll use Laravel because on top of PHP,
it just gives you more.
And just to teach you, then, for a moment, I'm going to go
into a v-host directory
into a sample application using Laravel.
At first glance, frankly, it's probably a bit overwhelming
because there are so many files, so many directories,
but the important thing, and as we'll see after a quick break,
is to focus only on the most germane things
to getting some work done initially, and that's going
to be in the apt directory.
As you'll see and hear, there are still a bunch of folders,
but the ones on going to care about, primarily, are this.
If I open up route stop PHP, notice as a teaser,
how this application is about to work.
This is a reimplementation in Laravel
of the super simple stupid course application
that gave me week zero, week one, lectures, and the PDFs.
I'm simply going to specify -- my screen is a little cut off,
again -- I'm going to specify the following,
that I want to define a route using HTTP get for slash,
and whenever a user for this application visits slash
on my website, I want to execute the anonymous chunk of code,
namely return view colon make.
So this means call a static method inside
of a class called view, and make renders a template
that is called index, and it passes in a bunch
of key value pairs, in this case, title.
Meanwhile, if the user visits slash lectures,
this code should execute,
and the lectures template should get rendered.
Finally, if the user visits lectures slash something,
where that something is, per the bottom here, a number,
one or more decimal digits,
then I want to render the lectures template,
passing in the title as well.
So the end result is that --
if I actually pull up this application in my browser,
the end result -- whoops.
The end result is exactly the same,
albeit with slightly sexier URLs now.
Now it is a lecture slash 0 instead of week zero dot PHP.
But, let's pause here.
Let's take a two-minute break, and when we come back,
Tim is going to come on up and introduce us
to the inner workings of Laravel and the kind
of functionality we are about to get out of the box.
>> All right, so, now we're going to be taking a deeper dive
into the Laravel PHP framework, which David mentioned.
So Laravel, the first place we're going to start is going
to the quick start
and installing it via something called Composer.
So Composer is a package management system
for PHP similar to gems for Ruby or Pit
for Python or MPM for No JS.
Composer helps to manage all the different dependencies
that Laravel depends on when -- sort of, under the hood.
It uses a lot of the symphony,
another PHP framework's inner workings.
And, so, we are going to use the commandline
that they give us here, Composer create project Laravel Laravel,
after we've installed the Composer utility,
which I have already done on the -- let's see.
So I'm using the CS 50 appliance
because this has all the PHP versions and my SQL
and everything that we need to get started running.
So I'm going to go ahead and pull up the terminal here
and jump into the v-host directory, and I'm going
to paste this -- oh, I can't do it that way.
So I'm going to SSH to the Linux instance
that I'm running in the VMWare.
And I'm going to run this Composer create project Laravel,
and I'm going to change your project name
to just example dot dev.
And behind the scenes,
this takes a little while the first time you're going
to do it, but it is installing all the dependencies
that you're going to need to run Laravel.
So hopefully this does not take too long.
It should just take a second.
The first time that you do it, it will take a few minutes,
probably, because it needs to get everything.
I have it all cached here.
So now that we have example dot dev, what we're going to need
to do to be able to access this
in our Chrome browser is go ahead and jump in to --
we're going to pseudo via the Etsy hosts file.
And we're going to go ahead and add a new line here with the IP
of the 50 appliance, which is shown here
in the bottom right-hand corner of the screen, the 192 168 --
and this might be different depending on your machine,
so 192 168 69 128 [audio break] project example dot dev.
Okay. Save that.
So, now, if we go to example dot dev, we see the -- let's see.
Oh, I was in the wrong directory, so let me go ahead
and move the example dot dev into the v-host directory.
And now if we refresh it, we see that it says forbidden.
You don't have access.
So we need to go into the example dot dev directory
in V-host, and we need to chmod it to 0711, the example dot dev,
as well as the public -- oh, I did that wrong both times.
So the example dot dev, as well as the public directory
that is underneath it, so that it is able
to get to the index dot PHP.
And now, if we refresh -- oh, we're still having problems.
So inside the example public directory,
if we list all the files, including the hidden ones,
there's an HD access file, and this is used by Apache
to help determine, for this specific directory,
where requests should be routed.
So this also needs to be chmoded to 0644, and now you've arrived.
So that was the quick install of Laravel via the commandline.
So let's go ahead, and I'm going to use something called Samba.
If you're not familiar with it, you can -- from the finder menu,
go to go and connect to server, and you can go ahead
and do SMB colon slash slash and the IP address
of the Linux instance that we're running in VMWare and go ahead
and connect to the server so that you can mount it
as file system and use it
as if you were locally connected to it.
So I'm going to go ahead and pull this
into an editor called sublime text.
If you're unfamiliar, it is a really nice editor
that is available to edit code files.
And so we're going to take a look
at the directory structure for the project.
So there's a few different folders in the top level.
There's app, which David has mentioned already.
There's bootstrap, which are some files used internally
by Laravel to bootstrap all the dependencies, load them all in,
make sure that all of the magic methods are called properly
to be able to [audio break].
There is the public directory, which is where all
of our public-facing assets, such as the index dot PHP
where requests are routed, images, CSS files, JavaScript,
anything that needs to be directly accessible
by the world lives in the public directory.
And then the vendor directory are all of the assets --
or all of the [audio break] under the hood that does a lot
of the heavy lifting that Laravel depends on.
So typically, in a public directory, this is sort
of what you would see, the CSS, JavaScript,
those files aren't there when you install the framework,
but those are things that you will likely want to add
as you're building out your applications.
The HD access file that I mentioned and the index dot PHP,
which is the front controller for the --
where all the requests go through initially.
So inside the application folder, these are --
there's actually a couple --
there are actually a few different folders here.
There's a lot of them, and as David mentioned,
it can be overwhelming at first, but you just want to concentrate
on these specifically.
So there is the routes dot PHP, which is what we saw earlier
to show that you can route a specific URL and HTP method
to a certain either anonymous function, as David was using,
or a Controller, which we're about to go over.
There's the CONFIG folder,
which is where you set any application configuration
that you need, such as database settings or any APIs
that you might be dealing with.
If you're using something to send mail
or if you're connecting to Amazon to store images,
you would set all of those configurations
in a central point in the application so that --
sort of conventions that everyone, as they're working
on a team, you don't have to think about where is this.
It's just you have a set place where it goes.
That would be in CONFIG.
Controllers are the place in the application
where you typically route requests.
So the controller handles a lot of the application logic
and sort of picks what the display of the screen is going
to be, the views, and then also shells
out to the different models which contain the data
and pass those to the view.
So you sort of -- the controller is sort of the dispatcher
for the requests, you know, what you're going to do
with the data -- or with -- depending on the request is,
what data should be shown and where do we get it from.
And the views are typically just HTML,
maybe using a templating engine, as we're going to go
over with the Blade templating engine that Laravel ships with.
And then the other directories for right now, we're just going
to sort of gloss over and not worry about.
But those include tests, so unit test-free applications,
filters, storage.
I keep doing this.
All right, jumping back over here.
So the first thing that it comes with is this route get
and it does view make hello.
So if we look in the views directory,
we see that there is a hello dot PHP,
and that's where this you have arrived is coming from.
But we can also change this to --
so, we can change this to hello, and now, if we try to go
to a route that is not defined we get this sort of stack trays
as to what went wrong with the application.
But if we go slash hello,
there is actually an internal server error.
So there is a little bit of magic going on behind the scenes
in the CS 50 appliance to make sure that you're able
to name all these directories conveniently.
The name of the -- so the example dot dev that we gave.
So we actually have to change one more thing
in the HD access file.
So we're going to go ahead
and add this line, rewrite base slash.
And that basically is another directive.
David, you can speak more to what the rewrite base does.
>> It essentially corrects more what the server thinks.
It's supposed to be [inaudible] virtual host.
So specifying to ignore that by rewrite base slash addresses.
>> Right. So, now, slash hello works properly.
So let's jump back over here.
So along with just doing an anonymous function like this,
there are -- so this is just a recap
of the MVC I was just discussing.
It goes into the routes and then shells out the controllers,
which deals with the models and views and how all
that should be dealt with.
So controllers are sort of -- help to centralize all of that,
and you can access them via routes by saying route get.
So we could say route get hello,
and then the controller name at and a method name.
So let's go ahead and give that a shot.
So we'll say route get slash hello,
and we'll do a hello controller at show hello.
So in order for this to work,
we need to add a file called hello controller dot PHP.
I'll go ahead and -- so it extends a base controller,
which Laravel already ships with,
which extends a controller, which is sort of under the hood
and does a lot of the stuff behind the scenes
that helps controllers to manage all this state
that is coming into the application.
So the home controller, we said it would be named show hello.
And we'll just -- instead of returning a view,
we'll just return hello world.
Oh, hello -- oh, I copied it.
Hello controller.
So hello world.
So that is just a really, really simple example
of how you can create controllers.