Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Extending a Collection7:34 with Alena Holligan
We'll be using multiple interfaces as we extend our content types. This will allow us to create different groups of functionality that will be used throughout my application.
Great job defining some additional interfaces. 0:00 We'll be using those interfaces as we extend our content types. 0:03 So far, the site only displays posts, but we want to show pages as well. 0:07 Each of these content types will have many things in common, but 0:13 they will also have their own features. 0:17 Let's get our site ready for these different content types, and 0:19 expand the post features at the same time. 0:22 Let's start by creating a new class in our source classes folder. 0:25 We'll name this Posts.php. 0:31 Our class Posts is going to extends Collection. 0:36 >> We'll need to change the constructor a little. 0:46 So let's add a new constructor to this class. 0:49 We're going to accept the same parameters as the parent so 0:52 we can call the parent constructor. 0:55 So let's go into the collection class, and select this constructor. 0:57 Before the call to the parent constructor, we need to set the entity or 1:06 type of content we'll be using. 1:11 This equals 'posts'. 1:13 And then we can call the parent::_ _ construct. 1:19 And we'll parse the $repo, the $id, and the $field. 1:25 Now let's change the constructor on the parent to use this new entity. 1:30 We're going to add another protective property for entity. 1:34 And then instead of posts, we're going to change this to ($this->entity). 1:41 Let's change our instance calls to make sure that this new class works. 1:53 Posts. 2:00 Posts. 2:03 And let's preview. 2:05 Great. 2:06 Everything still works as it did before. 2:09 We're ready to implement the new trackable and shareable interfaces. 2:11 Let's go back to post. 2:16 Posts is going to implement, 2:19 TrackableInterface and ShareableInterface. 2:24 Now for the TrackableInterface. 2:31 Public function getAuthor. 2:35 To get the author name we're going to use our repo again. 2:44 But this time we'll be searching in users, 2:48 $user = $this->repo->find('users, 2:53 and then $this->current()-> author). 2:59 This will find the user that matches the author id of the current item. 3:05 Because the find method returns an array, 3:09 we need to set the user variable to contain just the first item. 3:12 Now we're going to check if (empty($user->name)). 3:17 Then we can return 'Unknown';. 3:25 If we get past that return, we can return the user name. 3:29 Return $user->name;. 3:35 Now for the public function getDateTime. 3:40 We want to return a formatted string for the date and time. 3:50 We can add the format as an optional parameter so 3:54 that we could parse a specific format to this method, 3:58 even though the TrackableInterface did not define any required parameters. 4:01 Because this parameter is optional and 4:05 has a default value, it is still compatible 4:11 with the TrackableInterface, 4:16 $format = 'D, d M Y H: i: s'). 4:21 Now we're going to turn the current date time into a date time object. 4:25 $date = new DateTime($this->current()->dateTime);. 4:31 Then we can return the formatted string. 4:43 Return $date->format($format); 4:46 Now for the ShareableInterface. 4:54 We'll need to create three methods public function shareTwitter(). 4:57 To share on Twitter, we're going to return the URL encoded title with a space. 5:10 Return urlencode($this->current()->title. 5:16 And then a space. 5:28 Then we're going to link to the current page. 5:30 Http:/ / 5:36 $_SERVER [ 'HTTP_HOST']. 5:41 And then $_SERVER ['REQUEST_URI'] 5:48 Next we have public function shareEmail. 6:01 To share by email, we're going to return a string containing the subject and the body 6:10 return 'subject =', 6:17 urlencode($this->current()->title). 6:22 And then &body= 6:34 urlencode($this->shortDescription(). 6:38 Also add two line breaks "\n\nRead more at " and the link to our site. 6:50 And finally Facebook. 7:03 Public function shareFacebook(). 7:05 For Facebook, we'll simply return a link to the current page. 7:15 Let's preview our site again, just to make sure that we don't have any errors. 7:22 Great, our interfaces are implemented properly. 7:27 We're ready to start checking for interfaces and 7:30 displaying the proper views. 7:32
You need to sign up for Treehouse in order to download course files.Sign up