DEV Community

Roger Jin
Roger Jin

Posted on

Options for adding a blog to a PHP app

For more content like this, follow ButterCMS on Twitter and subscribe to our blog.

Imagine a situation you as a developer may face numerous times in your career. A client, friend, boss or who ever comes to you asking for add a blog to their existing website. They have a few requirements:

  • The blog must allow categories, tags, image uploads and other common blogging functionality
  • It must be easy enough for anyone in their organization to know how to use it and post new content
  • The blog must live at www.oursite.com/blog
  • The blog must match the look and feel of their current site
  • Their current site is built using PHP and they’d like to use the same technology

Given this scenario, there are a number of routes you could take. However, it’s important that you balance ease of use with the time required to get this up and operating. After all, this project has come with a deadline and budget as most do.

This particular client also happens to be fairly non-technical, so the system must be robust and require very little maintenance. Once it is set up, they want to be as self sufficient as possible. Let’s explore some options!

WordPress

This is often times the first system you might think of when it comes to adding a blog to a site. It has a plethora of features and plugins, it’s used and understood widely, and it’s free. It’s even PHP which would be another plus for the client! However, there are still quite a few concerns here.

The first concern is that deploying a WordPress install requires ongoing maintenance and security patches. Given the non-technical nature of the client, this isn’t ideal. Not only must the client then keep up to date with various updates, they must also handle any issues that arise from updating their install. The first time anything goes wrong with this, they’re going to come back to you asking you to fix it.

Choosing this route would also necessitate building and styling a theme that matches the current look and feel of the client’s site. This means that whenever the client updates their existing site you also need to spend time updating this separate site. This is an extra annoyance for both you and the client. Furthermore you now not only need to worry about the backend aspect of the code, you now also need to handle the frontend design and development as well.

In summary, WordPress certainly offers any and all functionality necessary for a blog. However, it also comes with some baggage of it’s own to be wary of making this option not so cut and dry. The extra features and abilities in this case not only add extra complexity overall, they aren’t needed by our client and will most likely only lead to additional issues to solve.

Roll Your Own

The first obvious downside to this approach is time. While this would certainly allow for a lot of flexibility in how the blog operates, it would also require designing and maintaining an interface easy enough for everyone in the client’s organization to use. Now not only do you have to worry about building a blog engine, you also need to worry about building an interface for a CMS to manage it.

Furthermore, you’re going to have to determine how and where to store media. If the client doesn’t already have a CDN to handle this, you now need to set up and explain to them how to manage it. If they do have a CDN, you need to spend time getting access to and understanding where within their CDN is best to store your content.

Why re-invent the wheel when there are already a plethora of open source options for a blogging engine?

Use An Open Source Project

Making use of an existing project is usually a great idea that saves a lot of time on projects like this. However, while this solution seems obvious on the surface given that, there are also other concerns.

Often times configuring and installing these systems is also relatively straight forward for a developer to handle. However, even using open source projects requires maintenance. As time goes on, dependencies of these projects may become out of date or insecure. They could also be incompatible with current versions of software you or your client are already using.

If the client asks for an update to their blog that your chosen library doesn’t support, you may then also need to spend time figuring out how the code works to add to it.

ButterCMS

With a solution like ButterCMS, many of the complications described earlier are taken care of for you. No configuration of servers is necessary, no upgrades are necessary, and an interface already exists. All the complexity of setting up any infrastructure for your blog is removed.

Instead, you can spend your time much more productively. ButterCMS can be added to your existing project via a simple Composer package. Then, simply start perusing through the expansive documentation to get your blog integrated with the site.

composer require buttercms/buttercms-php:2.2.0 (or current version)
Enter fullscreen mode Exit fullscreen mode

The design of the blog can make use of already existing styles and assets that the client’s site is currently using. When their main site is updated, their blog also gets updated with the same codebase.

Pulling in the content for the blog can be done with only a couple of lines of code. Below is an example of how this would be done with Silex, a PHP micro-framework based on Symfony components.

First, when setting up your app you’d configure any routes the blog will be using as well as an instance of the ButterCMS client.

use ButterCMS\ButterCMS;
Enter fullscreen mode Exit fullscreen mode

Then in the controller action tied to your routes, getting the blog posts can be done with a very small amount of code.

namespace MyBlog;

use Symfony\Component\HttpFoundation\Request;
use Silex\Application;

class BlogController {  
 public function listAction(Request $request, Application $app, $category) {  
 $client = $app['butter_cms']);  
 $posts = $client->fetchPosts([  
 'category_slug' => $category,  
 'page' => $request->query->get('page'),  
 'page_size' => 10,  
 ]));  

 return $app['twig']->render('list.html', ['posts' => $posts]);  
 }  
}
Enter fullscreen mode Exit fullscreen mode

Finally, in your view you’d simply loop through the posts to render them on the page using Twig.

{% for post in posts %}
 <h2>{{ post.title }}</h2>
 {{ post.body }}
{% endfor %}
Enter fullscreen mode Exit fullscreen mode

In essentially five or less lines of code you now have your entire blog up and running. All of the features required by your client are already built in for you. Pagination, category support, fast responses, everything your client asked for already exists.

Summary

Developers often face the situation of build it versus buy it when it comes to software. Either you can purchase access to a library of code for you or you can use something off of the shelf for free. Each has pros and cons. With a solution like ButterCMS and the benefits it provides are often times well worth the expense in time and complexity savings, especially in the long term.

This was originally posted here.

Top comments (0)