Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
Slim gives us this generic functionality we always need in a website, application, or api and none of excess code. If we need more functionality such as templating languages or the ability to send emails, it is extremely easy to add, by using adding on to Slim's existing code and making it custom for our needs. In this video we will discuss how to install Slim using composer.
Slim Version 2
If you are having issues, please make sure composer uses Slim version 2
"slim/slim": "~2.6"
Then run a "composer update"
404 error
Make sure you have a .htaccess file
-
0:01
There are many ways to look
-
0:06
at the model view controller, or MVC, way of building software.
-
0:11
To keep it extremely simple,
-
0:12
I offer you this basic example of a to do list application.
-
0:16
The user of your application or website will see your views.
-
0:20
Views will be the front end code,
-
0:21
such as HTML that the user sees when they arrive to look at the to do lists.
-
0:26
The views are also the form that allows them to fill in items to
-
0:30
add to the to do list.
-
0:32
When data needs to go to persistent storage, or
-
0:34
a database of some kind, such as storing the to-do list item that was just
-
0:38
entered into the form in your database, that code will reside in a model.
-
0:42
[SOUND] Which leaves us on how to connect the view code to our model code.
-
0:46
This is our missing link, controllers.
-
0:49
[SOUND] Controllers help us fill in the blanks to validate information that comes
-
0:53
from the forms to set up how we handle the logic between the view and the database
-
0:57
and manage any errors [SOUND] that might occur in the operation of our application.
-
1:02
If you like bullet points, here you go.
-
1:04
The model, it contains database-related code.
-
1:09
The view, [SOUND] it will contain our user interaction code.
-
1:13
The controller, it links the view code to the model code,
-
1:16
as well as a few other miscellaneous items.
-
1:19
As your experience grows, so will your understanding and
-
1:23
opinions of what MVC means to you.
-
1:26
In this course,
-
1:27
we'll concentrate on the part of the MVC pattern that the users of your site see.
-
1:32
The views, can we retired the tried and
-
1:35
true index.php file we've seen so often as our homepage?
-
1:39
No need.
-
1:40
Let just re-purpose it as a front controller.
-
1:44
As a simple definition, let us say that the front controllers
-
1:47
are a jumping off point for all of the traffic that comes to our site, so
-
1:51
that all requests and responses are directed through the index.php file.
-
1:56
Where they may eventually move to our controllers, serve up our view code, or
-
2:01
in the future, make updates to our database.
-
2:04
The index.php is our first step for our entire application.
-
2:09
To see this in action, we're going to use a micro framework as our base.
-
2:14
It's called slim.
-
2:15
You may be wondering what a framework is or do we really need one for
-
2:19
such a simple website.
-
2:20
Well, yes we do need one, but we really only need a microframework.
-
2:26
Speaking generally a framework is a software structure that provides
-
2:30
us with some generic functionality that can modify to our own needs.
-
2:35
Slim gives us this generic functionality we need in a website application or
-
2:40
API with none of the excess code.
-
2:43
If we need more functionality, such as templating languages or
-
2:46
the ability to send emails, it is extremely easy to add the code
-
2:50
that exists in Slim and making it custom for our needs.
-
2:55
Here is what Slim gives us from the start.
-
2:58
It gives us routing.
-
3:00
It gives us HTTP request and response items.
-
3:04
Caching, middlewares, sessions, and of course overkill Military Grade Crypto.
-
3:11
If you don't know what all of this is, try not to let it bother you.
-
3:15
I will explain it to you when we get to the implementation.
-
3:18
Let's get started by installing Slim.
-
3:21
So far, we have now installed Monologue and it been able to log to our
-
3:26
app.txt file all by using a package just from Composer, which is great.
-
3:32
We haven't had to write a logger, we have only had to use a few lines of code
-
3:36
to actually get some errors to log to a file for us.
-
3:39
Now that's great, but we need to install the core or
-
3:42
the base of what we're going to do for the rest of this project, and that is Slim.
-
3:47
Slim is a micro framework that we're gonna use for this project and for
-
3:51
any project going forward that needs a nice, stable base.
-
3:55
To do that, let's go over first and find out about Slim from their website.
-
4:01
So head over and open up a new tab, and I'm gonna type in slim framework.
-
4:09
All right, the first result for
-
4:11
slim framework is where we want to go, slimframework.com.
-
4:15
So on the explanation here, right up front, Slim is a PHP micro framework
-
4:20
that helps you quickly write simple, yet powerful web applications and APIs.
-
4:25
So it only needs a few lines to really get going.
-
4:28
So we'll get into this in detail as we progress through this project.
-
4:32
But what we're gonna start with is the user guide.
-
4:35
So inside of the user guide we'll see a get started and
-
4:39
in our get started we'll see installation.
-
4:42
So we're gonna install using Composer and we just simply write require Slim and
-
4:47
then two dot star which based off of the composer wild cards,
-
4:51
would allow anything that's a version two point and up.
-
4:55
So now let's go back over how we did it before, and
-
4:59
install it using interactive Composer.
-
5:02
So we'll go back to our console, and type in composer require.
-
5:09
Then when it prompts us, we type in slim.
-
5:13
Okay, we can't see all of it so let's make our console just a little bit bigger.
-
5:18
We will scroll up to the top and then type in zero for Slim.
-
5:24
Now we hit enter and then we can just take whatever the latest version is and
-
5:29
then it's going to pull it for us and get our require statements set up.
-
5:35
Once the require statement has been set up for 2.6, we're gonna hit Enter,
-
5:38
because we don't want to install anything else just yet.
-
5:41
So now it's going to put it in the composer json, and
-
5:43
then run the install for us.
-
5:46
So it's gonna look for any requirements from Slim, and add those as well.
-
5:52
Okay, so it's added everything that Slim needs.
-
5:54
I'm gonna clear my screen here.
-
5:56
It's clear, and then I'm gonna bring it back down to another manageable size.
-
6:00
Bring it down from here and
-
6:02
then take a look in our vendor folder we might need to refresh.
-
6:06
Here inside this folder is Slim and
-
6:09
all of the wonderful things that make up the slim framework.
-
6:12
We'll get started using it next.
You need to sign up for Treehouse in order to download course files.
Sign up