The Blog / Development / Using PHPActiveRecord with Slim Framework

Using PHPActiveRecord with Slim Framework

17 Oct 2011

When prototyping projects in PHP I normally use Slim Framework and for all database interactions I use PHPActiveRecord. These tools make Rapid Application Prototyping quick and easy to manage. In this article I will go through step by step explaining how to get these two up and running.

1. Downloading Slim

Go to the Slim Framework website and click on the Download and Install link on the homepage, download the latest stable version. At the time of writing the latest version is 1.5.1.

Slim Framework Homepage

2. Download PHPActiveRecord

Go to the PHPActiveRecord website and click Get Stable Build on the homepage, this should take you to the download page. Click on the latest stable release to download in your prefered format (zip or .tgz). At the time of writing the latest version is 1.0.

PHPActiverecord Homepage

3. Adding and Configuring PHPActiveRecord to your Slim project

In your Slim project root create a directory called vendor inside this directory add your PHPActiveRecord files, once this is complete do the following inside of your Slim index.php file after require 'Slim/Slim.php'

require 'vendor/ActiveRecord.php'; 
ActiveRecord\Config::initialize(function($cfg) {
    $cfg->set_model_directory('models');
    $cfg->set_connections(array(
        'development' => 'mysql://username:password@localhost/database_name'
    ));
});

4. Creating your Model

We need to create a directory in our project root called models (this was specified inside the config above), inside of this directory create your PHP Class that extend AciveRecord\Model

class Task extends ActiveRecord\Model { }

Your table name should be plural of your class name, so your table should be called tasks in your database. You can override this if you wish to. This is all documented in the PHPActiveRecord wiki.

5. Basic Crud

Let's read all tasks and create a task inside of our function in the Slim index file.

## Read
$app->get('/', function () use ($app) { 
    $data['tasks'] = Task::find('all');
    $app->render('task/index.php', $data); 
})->name('tasks'); 

## Create
$app->post('/task/new', function () use ($app) { 
    $task = new Task();
    $task->name = "My New Task";
    $task->done = 0;
    $task->save();
    if($task->id > 0) {
        $app->redirect($app->urlFor('tasks'));
    }
})->name('task_new');

We now have a task added to our database, notice how we call the Task model, we did not need to include it since it is loaded with PHP autoloading inside of the ActiveRecord library. Lets edit and delete a task

## Read
$app->get('/task/:id/edit/', function ($id) use ($app) {
    $data['task'] = Task::find($id);
    $app->render('task/edit.php', $data); 
})->name('task_edit');

## Update
$app->post('/task/:id/edit', function () use ($app) {
    $task = Task::find($id);
    $task->name = $app->request()->post('name');
    $task->done = $app->request()->post('done') === '1' ? 1 : 0;
    $task->save();
    if($task->id > 0) {
        $app->redirect($app->urlFor('tasks'));
    }
})->('task_edit_post');

## Delete
$app->get('/task/:id/delete/', function ($id) use ($app) {
    $task = Task::find($id);
    $task->delete();
    $app->redirect($app->urlFor('tasks'));
})->name('task_delete');

And that is how you would do a basic CRUD operation with PHPActiveRecord and Slim Framework, I kept this tutorial simple to allow for beginners to get off the ground easy with PHPActiveRecord and Slim. I did not use some of Slim's new features like

$app->map()->via();

and adding in conditions for the requested parameters

$app->get('/task/:id', function ($id) { 
    echo "Task number $id!";
})->name('task')->conditions(array('id' => '\d+'));

we also did not use both put and delete this time around either.

Conclusion

Both PHPActiveRecord and Slim Framework are a good set of tools to use for quick prototyping without worrying about too much configurations. You can create a RESTful application API with ease, Slim Framework is not a full stack Framework like Codeigniter or FuelPHP to name a few, but it is not meant to be. You can view and download the files for this tutorial here GitHub repository.

Social Media

Twitter
London Midland Train Square Bob Sponge Pants at o2 Arena The Reliable