Porting WordPress to Laravel – Json API vs WordPress apis

I am moving my site from WordPress to Laravel 4 as the site has moved away from being a blog and requires more enterprise and custom functionality.

The blogging capabilities of WordPress are Awesome, and I would like to continue using WordPress for this functionality, letting it do what it does best natively.

Read More

Justification

I have built wp-plugins to achieve basic levels of required customisations, however I feel as though the site has outgrown wordpress. I really need an MVC solution and a clear separation of concerns for my webapp, without the need to intermix OOP MVC code with wp/procedural code.

I don’t mind having 2 logins – wordpress backend & laravel backend…

Obviously, I would like the blog part of the site to look the same as the main part of the site.

I hope to achieve this by keeping installation of WordPress in a separate folder to the main application Laravel.

From here, I have two options, load up wordpress from within laravel and use the wordpress api or alternatively, expose a json-api or similar of the blog.

This will provide added benefit of allowing me to create an Android / IOS app and share content between main site & mobile devices.

Access WP via wordpress api

config/app.php – Example

...
define('WP_USE_THEMES', false);
require_once(Config::get('app.wp_path') . '/wp-load.php');
wp();
....

this will enable me to access the posts via wordpress api.

Access WP via json-api plugin

Using this plugin, I will be able to create a blog model and query the wordpress posts via curl & json requests.

controllers/BlogController.php – Example

class BlogController extends BaseController {
    public $restful = true;
    public function getIndex() {
        $data['posts'] = Blog::getPosts(1);
        $data['page'] = 1;

        return View::make('blog.index')->with($data);
    }
}

models/Blog.php – Example

public static function getPosts($page = 1)
{
    // get data from specified url via curl
    $url = "http://domain.com/api/get_posts/?page=" . $page;
    $posts = self::curl($url);   
    return $posts;
}  

The Question(s)

At present, I like the idea of exposing a json-api because of future extensibility etc. Furthermore, wordpress will only be loaded when required.

I also like the clean and consistent way in which my wp posts can be accessed and displayed within Laravel application.

Any comments / considerations in relation to the following?

  1. Application Performance
  2. Security
  3. Anything that I haven’t considered?
  4. Better way of integrating wordpress & laravel?

Related posts

Leave a Reply

2 comments

  1. You should try to keep WordPress’ blogging functionality separate to the rest of your webapp. Unless there is really good justification for integrating the WordPress API into your webapp (I am guessing you only want the blog/comment functionality?) you are going to be duplicating a lot of effort.

    What is wrong with having your blog skinned to look like the rest of the website, and simply just installing WordPress into a separate subdomain or just putting it into your public/ folder inside your Laravel app?

    If you need a json-api, you could either create a wrapper in your Laravel app around the existing WordPress API (which gets better with every release of WordPress).

    As you said, WordPress is amazing with it’s blogging capabilities. So why are you trying to replicate them by creating a new interface? That just seems like a waste of valuable time.

    There is no need to include WordPress to get access to an API, WordPress ships with an awesome XML-RPC API. The documentation can sometimes be a little lacking, but it is nothing that a little Googlefu or reading the source for the XML server can’t solve!

  2. This topic is maybe a little out of date. But i recently came across this repo.

    The guy uses the backend of WordPress and the frontend (mostly) Laravel. It’s really easy to setup and there are models for the basic stuff (posts, pages etc.). In this way you can easly re-create the blog functionality from WordPress.

    I would say give it a try. I didn’t try it yet, but definitely will when i got time.