Lately I've been doing a lot of development in Ruby, specifically with Sinatra and Sequel, and I've been liking that development stack a lot. So much so, that when I switch back to PHP for a project, I wish my PHP stack was more like my Ruby stack.
I managed to put together a few PHP projects that result in a very similar framework, described below.
Slim Framework
Slim Framework is a PHP micro-framework, very similar to Sinatra. I settled on this one because of its similarities to Sinatra.
<?php
$app = new \Slim\Slim();
$app->get('/hello/:name', function($name) {
echo "Hello, $name";
});
$app->run();
?>
PHP Savant
PHP Savant is a simple templating engine for PHP. After evaluating several "minimal" templating engines, I liked PHP Savant the best because of its simplicity.
It was one of the few that actually lets you write PHP in the views. Most try to create an abstraction where you have to learn a new syntax for outputting variables and writing "for" loops.
Here is an example controller and corresponding view code:
Controller
<?php
// Load the Savant3 class file and create an instance.
require_once 'Savant3.php';
$tpl = new Savant3();
$tpl->title = $name;
$tpl->display('books.tpl.php');
?>
View
<html>
<head>
<title><?= $this->title ?></title>
</head>
<body>
...
The great thing is that since the views are actually PHP, you can write whatever PHP code you want in them. You can do things like $date->format()
or simple foreach
loops without having to learn another syntax. The downside is the project hasn't been updated since 2011, although I haven't run in to any major issues with it yet. If you know of a similar templating engine I'd be happy to learn about it.
This is worth repeating: I am not interested in a templating framework that invents its own syntax. PHP, Ruby and Perl can all be used as templating frameworks, there is no reason to re-invent this. I do not want to use Smarty, Haml, Liquid, etc. These are all abstractions. I just want to write HTML with some variables stuck in it. PHP Savant and Ruby's ERB are the only ones I've found that do this.
Idiorm
Idiorm is the simplest PHP ORM I could find. It is very similar to Sequel for Ruby. It provides a simple wrapper around building queries.
<?php
ORM::configure('mysql:host=localhost;dbname=test');
// Simple Queries
$numUsers = ORM::for_table('users')->count();
$users = ORM::for_table('users')->find_many();
foreach($users as $user) {
echo $user->name;
}
// Inserting a Record
$user = ORM::for_table('users')->create();
$user->username = 'bob'; // setting properties
$user->set('age', 25); // the set() method
$user->set_expr('created_at', 'NOW()'); // setting values as expressions
$user->set(array( // setting bulk properties
'first_name' => 'Bob',
'last_name' => 'Smith'
));
$user->save();
?>
This ORM does not provide concrete classes for your database tables. If you're looking for something more like Ruby's ActiveRecord or DataMapper, then you can also try Paris which is built on top of Idiorm.
The Full Stack: Slim/Savant/Idiorm
Put together, this stack is very similar to a common stack in Ruby. The table below shows the comparable Ruby and PHP components.
Component | PHP | Ruby |
---|---|---|
Routing | Slim | Sinatra |
Templating | Savant | Erb |
Database | Idiorm | Sequel |
Projects I've written with this stack | IndieNews |
IndieAuth Pingback Flickr Archiver |
Front-End
It's worth noting that this stack will work with any front-end environment. You can use any CSS or Javascript framework with this, this environment doesn't care.