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 Designing Interfaces in PHP!
You have completed Designing Interfaces in PHP!
Preview
Our repository interface provides the ability to find a single item. In this video we will rely on the methods defined in the interface to display a single blog post.
This video doesn't have any notes.
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
We already have the ability
to find a single item.
0:00
So let's write out the rest of what is
needed to display a single blog post.
0:03
First, let's create the single post view.
0:07
Add views>New File>Single.php.
0:10
We'll add the opening and
closing article tags.
0:20
Again, you could check to make sure
that this post status is public.
0:31
Since this is a single post, we can
let the post title be the page title,
0:36
so we don't need to add that here.
0:41
All we need to show is the item details.
0:43
Because this is a single item,
we don't really need to loop.
0:46
All we need to do is grab
the details of the current item,
0:50
which will be the first and
only item in the collection.
0:53
Echo $content->current()
0:59
->details;.
1:06
Now we can link the post titles
from the list to their own post.
1:08
Back in views list We're
going to add a href=.
1:14
We'll use the current page but
we'll add id=
1:22
The item id.
1:29
Now let's go back to our index page.
1:48
Right after including the config file,
let's check for an ID being passed.
1:53
We can then use that ID
to find a single item.
1:58
if (isset( $_GET
2:02
['ID'] ) ).
2:07
We'll add content = new Collection.
2:12
Again, let's add some
lines to space this out.
2:19
We're going to pass the repo, and
then we're going to pass the id.
2:25
We don't want to use this id directly,
2:30
instead we want to make
sure we filter input.
2:34
INPUT_GET, the id, and
2:39
then FILTER_SANITIZE_NUMBER_INT.
2:45
Next, we're going to validate that
2:53
we have the single item
that we want to use,
2:59
if (isset($content) &&
3:06
$content->count == 1 &&
3:11
$content-> current()->status
3:16
== "published").
3:23
Then we want to set the current $title =
3:31
$content->current()->title.
3:36
Else, we'll select all the content and
3:43
keep our title Treehouse Blog.
3:48
We're going to add one last check
to see which view we should show.
3:53
If ($content-> count()
3:58
==1, then we'll include
'views/single.php';
4:05
Else, We'll add our foreach loop.
4:17
And let's go back to preview.
4:24
Now when we click on a title,
let's jump back to work spaces.
4:27
This is a call to a method,
not a property.
4:33
And we see our entire blog post.
4:38
Our blog site is looking good.
4:40
Before we move on,
4:42
let's take a little more time to
explore the use of interfaces.
4:43
In the following challenge,
4:47
you're going to build two interfaces that
we'll be using later on in the project.
4:49
Now that we're able to display
posts on our blog site,
4:54
what kind of interaction
do we want to support?
4:57
For blog post, it can be helpful to track
an author associated with that post,
5:00
as well as a date that post was published.
5:05
We could say that we want
post to be trackable.
5:08
So we'll define a trackable interface.
5:12
We may decide to make another other
piece of content trackable, like images.
5:14
Using an interface will allow us
to interact with these objects
5:19
in the same way.
5:23
Another important feature of a blog is the
ability to share a post on social media.
5:24
Besides post, we may wish to share other
content such as authors or even images.
5:30
Once again,
we can create a shareable interface
5:37
which will define what behavior is
required for an object that is shareable.
5:40
Go ahead and give the challenge a try.
5:46
I'll be giving you directions
throughout the process.
5:47
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