The Problem with Comments

The main thing missing from the project right now is the ability for users to leave comments. Unfortunately, blog comments have multiple issues with them.

First of all there’s the moderation, approving, and general management of the comments. Yes, with Laravel 5.1 we could add this functionality to the blog’s administration, allowing users to sign up, allowing the users to make comments and so forth. It’d be easy enough to create management screens to handle all of this.

Adding Disqus Comments

Creating a Disqus account

Head over to Disqus.com and sign up for a free Disqus account. Once you have an account, you’ll want to click on the link to Add Disqus to your site(If you can’t find the link, try clicking on the gear icon at the top right of the screen.)

You should see an image like the one below.

Figure 15.1 - Add Disqus to your site

After you fill out the simple form, choose Universal Code from the next screen. Then you’ll see a screen like the one below.

Figure 15.2 - Disqus Universal Code

Note the value of the variable disqus_shortname in this code.

Passing the Slug to the page

Next we’ll add the variable $slug to the pages showing posts. To do this modify the one line in the showPost() method of BlogController as instructed below.

Be sure an use the correct shortname noted earlier for SHORTNAME HERE in the above snippet.

If $slug is set then it sets the disqus identifier to group all the comments together for that page. $slug should now be set for all post pages.

You’ll also note that the whole template is wrapped in a big @if statement. This is because I didn’t want to show comments when I’m working on the blog locally. Feel free to change this to suit your needs.

Updating the Footer

Finally, update the page-footer.blade.php in the resources/views/blog/partials directory to match what’s below.

BlogController updates for the RSS Feed

// Add the following use statement at the top of the file
use App\Services\RssFeed;
// Add the following method
public function rss(RssFeed $feed)
{
$rss = $feed->getRSS();
return response($rss)
->header('Content-type', 'application/rss+xml');
}

That’s it. Point your browser to http://l5beauty.app/rss and you’ll see the feed.

Create a Site Map

To keep our blog search engine friendly we’ll add one final feature to the blog … a Site Map.

We’ll use the same technique as we did with the RSS Feed, to build it on-the-fly, but cache the results so it’s only rebuilt a maximum of once every couple hours.

Creating the SiteMap Service

Create a new file named SiteMap.php in the app/Services directory with the content below.

Adding the sitemap.xml Route and Method

Addition of sitemap.xml route

// After this line
get('rss', 'BlogController@rss');
// Add the following line
get('sitemap.xml', 'BlogController@siteMap');

Then update the BlogController as instructed below.

Updates to BlogController for SiteMap

// Add the use below to the top of the file
use App\Services\SiteMap;
// Add the following method
public function siteMap(SiteMap $siteMap)
{
$map = $siteMap->getSiteMap();
return response($map)
->header('Content-type', 'text/xml');
}

And point your browser to http://l5beauty.app/sitemap.xml to make sure it works.

It’s A Wrap!

That’s the end of the l5beauty project. You’ve developed a complete
blogging system in Laravel 5.1 and hopefully have learned a lot along the
way!

Recap

This chapter put all the final pieces together for the blog. We started using Disqus for comments and then created a RSS Feed for the project. Finally, the project was finished by adding a Site Map.