00:01
Well, right now, your route in this web PHP routes file, it's doing everything.
00:08
It's handling the request, it's preparing data, it's returning a view, and it might
00:12
not seem like it's doing that mostly because Laravel does all that for you
00:16
in this route facade that's getting this slash directory and it's returning a view
00:22
and it's simple, but it's doing a lot.
00:26
So what does this look like?
00:27
As your application grows, you'll want a better way to organize things, and
00:31
that's where the MVC model comes in.
00:37
Model view controller.
00:39
And if we're gonna take this into an analogy, let's think of a restaurant
00:43
while MVC is not necessarily the order that everything happens in
00:47
terms of model and then view and then controller within Laravel, let's.
00:52
Talk about the controller first.
00:53
This is what you would think of as the waiter in a restaurant.
00:57
It takes your order, it coordinates everything, and
00:59
then it brings you the food.
01:02
Well, the kitchen would then be the model.
01:04
This is what prepares the food, it manages the ingredients, the, your
01:08
data, I guess, in an application.
01:10
And then there's the presentation, how the food looks on the plate.
01:15
It's, it's your view.
01:17
What this looks like in Laravel terms, the controllers handle your requests.
01:21
It coordinates responses while your models manage your data and your business
01:26
logic, and then your views actually display the information to your users.
01:30
Now, we've already seen what a view looks like.
01:32
Each one has its own job, but your controller and your model
01:36
haven't necessarily been shown to you in this course just yet.
01:40
So let's go ahead and create your first controller.
01:42
That way you have a little bit more insight as to how this all
01:45
fits within Alar application.
01:47
Just for simplicity's sake, why don't we move everything that we
01:50
have in this routes file route gets, and then return view of home.
01:56
Why don't we move this logic to a controller?
01:59
We're gonna generate one now we can do this in our terminal for
02:02
application, or we can actually just do it in the terminal, built into
02:05
your code editor if there is one.
02:07
I'm gonna do that here just for simplicity's sake.
02:10
We could run PHP Artisan and then a command.
02:16
Now I am going to tell you what this command is, and then we're gonna take a
02:19
look at why this command actually exists.
02:23
We're gonna say PHP Artisan make.
02:27
Then we can give a name of a controller here.
02:32
PHP Artisan that is lar v's.
02:35
Way of running specific commands.
02:38
It's Lev's command line tool.
02:40
It's your Swiss Army knife.
02:43
I guess for Laravel development.
02:45
We use it a lot to create things, not just controllers, but there are a ton
02:50
of artisan commands that are available.
02:53
To be able to run anything within Laravel, and the cool thing is you
02:58
can actually create your own as well.
03:01
For right now, we're just gonna be using a lot in this make directory of
03:05
PHP Artisan commands, mostly to make controllers, and later we'll make models
03:11
maybe database migrations as well.
03:14
Artisan commands are incredibly powerful, so there's so much to dive into.
03:19
But for now, let's go ahead and make a controller, PHP Artisan
03:24
Make Controller, and we don't have to add anything here afterwards.
03:28
If we press enter, it's automatically going to prompt for what that
03:32
controller should be named.
03:33
You can even see what that looks like typically
03:37
in terms of casing and in terms of how it should be spelled and maybe handled
03:42
with an layer of application because we are going to be creating chirps.
03:46
We want a chirp controller, so chirp controller.
03:51
We're gonna stick with an empty controller for now, but later we'll go back and look
03:56
at what resource actually provides for us.
03:59
And you can see it created a specific file for us.
04:02
This app, HTP controller's, chirp controller.
04:05
Alright, let's go ahead and open up that new file that I created.
04:08
Again, that's app, HTTP, controllers, chirp controller.
04:14
It's just a blank file, but let's go ahead, add the things that
04:18
we had in our web route here.
04:21
We're gonna add an index method, that's public function index,
04:26
and then we're just gonna return a view.
04:28
We're gonna return a view of home.
04:30
Don't forget the semicolon.
04:32
And you can see here that our VS code, Laravel extension still takes
04:36
us directly to that view that we have.
04:39
So in our web routes,
04:40
how do we change this to instead point to our index method in our chirp controller?
04:46
Let's go ahead and clear this out\ so instead of returning a view here,
04:50
we're actually going to use the chirp controller or importing that in.
04:55
It's gonna be a class and then we can pass it a particular method.
04:58
In this case, I'm going to be direct and pass it that index method.
05:02
Let's take a look at our browser
05:04
refresh, everything still works great.
05:07
This is just a cleaner way of writing what we were doing directly in our routes file.
05:12
But now all of this lives in our controller.
05:15
Now we can slowly start to add to this controller as we
05:19
build out our application.
05:20
Now controllers really shine when you need to prepare data and then serve that data.
05:25
Again, similar to how a waiter might do this in a restaurant, we're gonna add
05:30
some sample chirps to see this in action.
05:33
So in our index method, I'm going to add a chirps array, and this
05:38
is just some sample chirps with a.
05:41
Time and author, and then a message.
05:43
Now, how do we pass this into this view?
05:45
Our second parameter is going to be an array.
05:48
We're gonna say that this is
05:50
chirps and that chirps is going to be equal to the chirps array variable.
05:57
Now, how do we display these chirps in our view?
06:00
Again, if we're talking about the MVC model, view controller structure,
06:04
we have our controller and we're passing these chirps to our view.
06:08
Now, how do we display them here?
06:11
Well, this is where that blade templating comes into play, so a home blade PHP file
06:18
is not just HTML, it is Blade, and we can add some templating structure to this.
06:23
In this case, for every chirp we can say, hmm, maybe for each chirps as
06:31
chirp, and then every single instance of chirps that we have in that array.
06:37
That is being passed in from this CHIRP controller.
06:41
We're going to iterate over whatever HTML that we're passing in here.
06:46
I'm just gonna say end for each here to say that this is
06:50
the end of that for each loop.
06:52
Now we have available the chirps array.
06:55
We have the chirp author, the chirp message, and the chirp time.
07:00
I am going to paste that in here.
07:04
That we're looping through the author, the message, and the
07:08
time to coincide with what we're passing in to the trip controller.
07:12
Let's see what this looks like in our browser.
07:14
That looks pretty good.
07:15
Now you can see how our controller is going to allow us to pass additional
07:19
data to different views, and we can also have different methods.
07:24
Let's take a look at what that looks like.
07:27
What I'm going to do is I'm actually going to copy all of this and delete
07:31
this chirp controller, because when we used that initial method to create
07:36
a controller, we just selected empty.
07:38
What happens if we then create a chirp?
07:40
Controller that is a resource controller.
07:44
This is what you might typically use in a standard MVC structure
07:49
model, view controller.
07:50
This resource allows for an optional model that you might have.
07:55
We don't have a chirp model just yet, so I'm not going to select anything there.
08:00
Now, in that chirp controller, we have seven different methods.
08:05
The index, create store, show, edit, and update and destroy.
08:11
These are all methods that you would probably typically use within
08:15
any standard MBC application.
08:18
Anytime we use an artisan command, we don't necessarily need to leave it blank.
08:23
We could write chirp controller and then even pass it additional,
08:28
uh, options or parameters.
08:30
Most artisan commands have this ability where I could pass it as the resource
08:35
and it would automatically complete what we just had completed for us.
08:40
Now you might start to see how each one of these seven different methods that
08:43
we have within a resource controller can be used within our application.
08:47
For example, the index, it's displaying a listing of all resources.
08:51
It's listing all of the chirps, this home page.
08:55
The create method might be showing a form to create a new
08:59
chirp in our demo application.
09:02
We're doing this on the same page, so we likely won't use this.
09:05
The store where we're creating and saving a new chirp the public function show
09:10
to be able to display one single chirp edit, displaying a form for editing.
09:17
A single chirp update would be to update that single chirp.
09:22
Then destroy, deleting a chirp.
09:25
We're going to be using most of, if not all of these, eventually
09:30
throughout our application, but for now, we're just using the index method.
09:33
So to recap, here's what happens when someone visits your site.
09:37
This route, this slash parameter, this root of your application is then
09:43
going to pass the chirp controller, specifically the index method
09:47
of this chirp controller class.
09:50
The controller prepares the data, it's waiting our table to decide how
09:54
are we going to present this data?
09:56
And the controller passes all this information to the view, in this case,
10:01
that home blade, not PHP, where that view then renders that HTML with the
10:06
data and then the user sees this page.
10:10
So why does all this matter?
10:11
Well, right now we're using fake data, but imagine when we have thousands
10:14
of real chirps in a database, the controller that we just set up.
10:17
It's going to decide which chirps to show.
10:20
It's going to take our order and then decide how.
10:24
To present them the model, which we're gonna learn about in the next lesson,
10:27
it fetches them from the database.
10:29
Then the view that home blade php, at least for our index, displays them nicely.
10:35
So each piece has its job and the MVC structure model, view controller,
10:39
your code stays organized, and then future you will, will thank
10:43
current you for how things are organized and displayed to your user.
10:48
I. So in our next lesson, let's start working with real data to see how your
10:52
app might remember things permanently.