PHP Articles

Read the latest articles in the 'PHP' category.

Slim Twig Eloquent PHP

Using Eloquent, Twig and Slim PHP - Revisited

It’s been about one and a half years since I made my blog post Using Eloquent, Twig and Slim PHP and a lot’s changed since then but I still love using these three tools for small websites.

You can view all the code for this post here or if you’re looking for the original code from 2013 please switch to the 1.0 release.

Mitch

PHP Traits

PHP 5.4 Trait Qwerks

Although the title may lead you to think otherwise; I actually really like Traits. I wish I had made the switch to 5.4 sooner! Bundle them with the shorter array syntax and I'm a happy PHP dev.

I recently experimented with Traits in a Laravel package. You can find the package here DatController

This blog is contains a couple of the not-so-obvious points of traits which I came accross.

You can not override trait properties

I think this is what I like the least about traits. You can not set a default property and have a class override it.

<?php
trait NewTrait {
	public $variable = 'default value';
}

class NewClass {
	use NewTrait;
	
	public $variable = 'override value';
}

I would love to be able to override trait properties. However, you could argue that it's best to use inheritence for these situations.

Laravel Laravel 4 PHP

Creating a Twitter Feed in Laravel 4

This tutorial is now out of date - checkout philo/laravel-twitter or thujohn/twitter for an easier and more up to date solution.

In this tutorial I aim to show three things

  • How to get Twitteroath working with Laravel 4
  • How to create and access a new configuration file for your twitter settings
  • How to create a helper method for your tweets

To start off install twitteroath via composer.

Put line 4 in your composer.json with the rest of the Laravel Composer set up and run composer update or composer install if you haven't already

{
	"require": {
		"laravel/framework": "4.0.*@dev",
		"abraham/twitteroauth": "dev-add-composer-json"
	},
	"autoload": {
		"classmap": [
			"app/commands",
			"app/controllers",
			"app/models",
			"app/database/migrations",
			"app/database/seeds",
			"app/tests/TestCase.php"
		]
	},
	"scripts": {
		"post-update-cmd": "php artisan optimize"
	},
	"minimum-stability": "dev"
}

Eloquent Slim Twig PHP

Using Eloquent, Twig and Slim PHP

Note: See the update to this post where I've made several changes

Originally while I was writing this blog I intended to make a step by step guide to putting together three of my favourite PHP tools. After a few issues and finding out that Slim Views is now a thing I decided instead to put together a git repo so anyone can get started quickly. I will use this blog post to quickly show how it works!

*Note that this tutorial doesn’t aim to teach you Slim, Twig or Eloquent

  • It’s to show you how to use them together effectively.*

You can find the repo here: https://github.com/acoustep/slim-twig-eloquent

After following the Installation instructions you should be able to use each component as well as Eloquent validation.