Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed Introducing MVC Frameworks in PHP!
You have completed Introducing MVC Frameworks in PHP!
Preview
Step through the requirements for a Minimum Viable Product using the Slim Framework.
Slim Framework Documentation
Additional Courses
Setting up a Local Development Environment Dependency Management with Composer
.htaccess File
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [QSA,L]
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
Let's start at the Slim Framework's
website, which is slimframework.com.
0:00
On the first page, they give us the bare
minimum needed to run a site with Slim.
0:05
This will be our minimum viable product.
0:10
Let's copy this code.
0:12
This code makes a few assumptions.
0:14
For example, that you already
have a functioning web server.
0:16
And you've installed the required
packages using Composer.
0:20
We'll be using MAMP for our web server.
0:23
If you need to install Composer or
0:26
MAMP, check the notes
associated with this video.
0:28
We'll walk through installing Slim and
running the project with MAMP.
0:31
We'll also walk through each
line of code from this example.
0:35
If at any point you feel confused, check
the notes associated with this video.
0:39
First, I'm going to open Atom,
this is my text editor.
0:44
I'm gonna say, File > Add Project Folder.
0:48
And I'll call this slimmin.
0:52
And Open.
0:56
From here,
I can create a new file named index.php.
0:57
And here I'll paste in the code
from the Slim Framework homepage.
1:04
Next, we need to use Composer to install
Slim, so let's open a terminal window.
1:10
From here, I cd into Documents/slimmin.
1:16
This is my project directory.
1:23
Because I have Composer
installed globally,
1:24
I can type composer require slim/slim.
1:29
No, I want to set up a new Composer
file within my slimmin directory.
1:35
Composer installs the packages Slim needs.
1:43
Now we can go back to our text editor.
1:46
Here I can see that we now have
a composer.json and a composer.lock file.
1:49
We also have a vendor folder.
1:55
We're ready to step through our code.
1:57
These first two lines show that we're
using the PSR HTTP message package for
2:00
the Request and Response object.
2:06
This allows us to interact
with the request and
2:09
response class more easily by assigning
them a short name of Request and Response.
2:11
Next is our autoload file.
2:17
This will autoload any required classes.
2:19
Then we're ready to set
up our Slim application.
2:22
We create a new application on line 7,
then we specify our first route.
2:25
This will be a get route.
2:31
Our first property is our route,
2:34
which will be /hello/ the name
that the user types in.
2:37
The second property is the function
that handles our request.
2:43
We pass in the Request and the Response
along with an array of arguments.
2:48
We set name equal to $args['name'].
2:54
And then we send a response.
2:58
We getBody, and we write Hello,
and the name they passed.
3:00
And then we return that response.
3:05
Finally, down on line 16,
we're telling our app to run.
3:07
I'm going to use MAMP
to run a local server.
3:12
Open MAMP.
3:15
Under Preferences, go to Web Server,
and Select slimmin.
3:17
This is the folder that contains my
index.php file, and I choose Select.
3:23
I choose OK, and start the servers.
3:30
From here I can open the WebStart page.
3:34
I choose My Website.
3:37
We get an error because Slim uses
a controller to manage the site,
3:40
and there are two more
steps that we need to take.
3:44
First, when using a controller, instead
of each file handling everything itself,
3:47
we want all paths to route
through our controller.
3:53
To do this, we'll need to set
up an HT access redirect.
3:57
Back in Atom, we're going to
add a new file named .htaccess.
4:01
You can paste in the content I've supplied
in the notes associated with this video.
4:09
The first line tells
the server to turn on rewrite.
4:13
The second line gives the base
of the current directory.
4:17
The third line specifies
our first condition.
4:21
If the specified file from the URL doesn't
exist, proceed to the rewrite rule below.
4:24
The fourth line specifies
our second condition.
4:30
If the directory from
the URL doesn't exist,
4:34
also proceed to the rewrite rule below.
4:38
The fifth and
final line describes the rewrite rule,
4:41
direct the request to index.php.
4:46
QSA means that if there's a query
string passed with the original URL,
4:49
it will be appended to the rewrite.
4:53
L means that if the rewrite rule matches,
4:55
don't process any more
rewrite rules below.
4:58
Let's go back to the browser.
5:01
We still receive an error,
because we have only specified one route.
5:03
From the Slim Framework page, we can see
that this one route is hello/{name}.
5:08
So I will type /hello/alena.
5:15
Now we see, Hello, alena, and
5:22
we have our first project using
a framework with a controller.
5:24
Great job working through setting
up a minimum viable product
5:30
using the Slim Framework.
5:34
We used Composer to install
the packages we needed.
5:35
Set up our htaccess redirect so
5:39
that our controller can handle the routes,
and set up our first route.
5:41
Now that you have an overview
of the basic concepts,
5:46
we're ready to start
building an actual website.
5:49
We'll be looking more
closely at routing and
5:53
organizing our project for an MVC model.
5:56
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up