Adding Eloquent to Themosis (wordpress)

At my current job, @NetRelations, I was hired as a PHP/Wordpress developer (and front end dev). So naturally I do WordPress stuff, but working with WP can sometimes feel… Well *sigh*, because of the way the code in the popular, fantastic cms. That is why things like Themosis are so fantastic.

As a fan, user, supporter and Artisan of the great PHP-framewoerk Laravel , Themosis is like a warm, cozy blanket over WordPress! And we are purely talking code now, not the application it self. Themosis implements many features and ideas from Laravel, modern PHP and staying true to everything WordPress.
But this is not an intro to Themosis, this post is about getting more Laravel magic to your WordPress/Themosis setup.

Because yes, there are some things “missing” if you will. At least of your used to building sites and apps with Laravel. So lets get to it and implementing support for using Eloquent.

First we need to add Eloquent to our composer.json file in our root folder. So open your terminal and in your root run.

composer require illuminate/database

You should now see it in your composer file in the “require” object. Now comes the part which I’m not really sure what is the best way or best place to put it. That is loading of Eloquent and handing your database settings to it. I chose to put inside environments config files, so for my local installation I put the following code in side “config/environments/local.php”.

use Illuminate\Database\Capsule\Manager as Capsule;

$capsule = new Capsule;

$capsule->addConnection([
    'driver'    => 'mysql',
    'host'      => getenv('DB_HOST'),
    'database'  => getenv('DB_NAME'),
    'username'  => getenv('DB_USER'),
    'password'  => getenv('DB_PASSWORD'),
    'charset'   => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix'    => ''
]);

$capsule->setAsGlobal();
$capsule->bootEloquent();

What are the getenv() things in that code. Well if you use Themosis or open the file I mentioned, you will see that they are used for the WordPress database settings. So you can just reuse those code parts.
One little disclaimer here right up front. You cannot remove the WP db-settings, as far as I know because WordPress needs a its database connection.

We have Eloquent added, installed and booted – now what?

Now we can just create our models and make them extend Eloquent, like this.

<?php
use Illuminate\Database\Eloquent\Model as Eloquent;

class PostModel extends Eloquent{

	protected $table = 'wp_posts';
	protected $primaryKey = 'ID';

}

Then in your controller you could do something like

public function allPosts(){
  $allposts = PostModel::all();

  return $allposts;
}

Not the most beautiful code but you get the gist.

So yeah that was basically it for adding Eloquent to your Themosis based WordPress installation! 🙂
But wait, just one more thing 😉

This last tip is something I’m going to try to make time to make a pull request for and implemented into Themosis.
So what I wanted when I added Eloquent to my project was to use the relationship features of it. At first I didn’t really get them to work as Eloquent (or what not) couldn’t figure what model I was referencing. But after a few minutes I figured I’d try something that I had tried earlier for another reason but failed. You see to be able to use your controllers and models in Themosis you have to ad them to an config file which in turn loads them in your app. Easy enough but a bit cumbersome, *cough* when your used to autoloading a la PSR4 *cough*.

Said and done, opened my composer.json and added a PSR-4 section to the autoload object and pointed it to my “theme/app” folder, like so.

[json]
“autoload”: {
“psr-0”: {
“Thms”: “library”
},
“psr-4”: {
“MyTheme\\”: “htdocs/content/themes/my-theme/app”
}
},
[/json]

Now you just need to add a namespace declaration to your models, and use the complete path when adding a relation.

<?php namespace MyTheme\models
use Illuminate\Database\Eloquent\Model as Eloquent;

class PostModel extends Eloquent{

	protected $table = 'wp_posts';
	protected $primaryKey = 'ID';

   public function tags() {
   /* Given that the related models name is TagModel */
      return $this->hasMany('MyTheme\Models\TagModel');
   }

}

As simple as that, don’t you think. And as a bonus, you don’t need to define your models in the “loading.config.php” file anymore as they are autoloaded. at least I don’t think you do, works for me so far.
Same should apply for your controllers, just don’t forget to put in the namespace declaration and “use” the files you, well use inside your class!

That is all for now. Comments, suggestions and improvements on the above is welcome.

Read more about EloquentThemosis

Need some humour in between the lines of code? Check out these  CSS puns !

frontend frameworks compared

So you are planning a new, shiny website. You want to use a framework as help to get you started, but which one is right for you?

WP Coding Standards
Good resource if your doing wordpress themes, plugins or whatever.

Just wanted to share this site with some awesome photography, I got linked the other day –  http://indulgd.com/
Enjoy!

Music Twitter – how did I miss

So apparently, some months ago Twitter launched something called Music Twitter.
I’m not really sure what it is, but from my first sightings it seems to be some kind of music discovery service. Or?

You can listen to short previews of songs with links to iTunes, and you can log in with Spotify or Rdio to listen to full tracks.
How did I miss all this, haven’t heard a word of it until today when I saw this article on TNW about it “quickly fading into obscurity”.
Well well, now You know too!

music.twitter.com

small BIG update- I’m a dad!

“Where is the continuation of the Laravel tutorial series? “, the people cried…
Calm down, something huge has happened. The reason the article series abruptly stopped is because a week ago my girlfriend went into labor and a few hours later my son was born. Yes people of the internets, I am a father!

In short, last week was spent settling in at home with the new family member, changing diapers and not getting that much sleep.
But worry not, between feeding little Theo and putting him in the crib (without waking up, which is a challenge), I have the nights to myself and will get back to finishing the Laravel series. If you haven’t setup Laravel 4 yet, do it now cuz part II is coming up.. 🙂

Get started with Laravel 4

This first part is less about Laravel and more about Git and Composer. We will set up our local Laravel installation och push it to Github.

This post-series is written in Swedish and you can find it by clicking the link to the right –> [with the Swedish flag]


 

Update: Dec 2014

As this post get tweeeted out, I feelI should say that it is an old article (series) I started summer 2013, but never got finished.
I am planning on a new Laravel 5 series, also in swedish, during January. So come back soon 🙂

Laravel PHP Framework

coming up: A small Laravel tutorial

Yes, here we go. I am going to embark on my first ever tutorial (series) adventure, and it will be on the great Laravel PHP framework.
Now a few things I want to note: first of I have never done this before, so it will be a learning experience for myself. I’m hoping to get some of you interested in Laravel and at the same time deepen my own knowledge in the framework. Very egoistic right 😉

Second, it will be in swedish. Oooooh please don’t cry, there is a reason for it.
Even though most of this site will be in english, from time to time I will make some swedish elements. Reason being, there is so much stuff out there for you english speakers already. Swedish people are generally very good at english, but some do prefer to get their info in their “mother tongue”.

I’m working on the first, and second, part at the moment and it will hopefully be out this week, but the swedish midsummer holiday is on friday so will have to see. I might end up just barbecuing and having some beers.

STAY tuned…

First look Windows 8.1

Microsoft has made a video with a first look at the some of the new stuff coming in Windows 8.1.
Take note every techblog and site on the internet – the startbutton is not an important and prominent feature!!!