Rack for PHP

Installs: 647

Dependents: 3

Stars: 68

Watchers: 4

Forks: 3

Open Issues: 4

Language: PHP

0.5.2 2015-07-10 01:39 UTC


Rack'em is an attempt to provide the awesomeness that Rack has brought Ruby, to PHP. See the documentation and more.

Latest Stable Version

# config.php
return \Rackem::run(function($env) {
    return array(200, array("Content-Type"=>"text/html"), array("Hello, from Rack'em!"));
$ vendor/bin/rackem
$ open http://localhost:9393


  • Tiny
  • Provides a common interface for applications
  • Painlessly stack application logic using "middleware"
  • Environment values are consistent regardless of web server (see Rack specification)
  • Run applications locally without other dependencies

Getting Started

Rack'em likes Composer, go ahead and install it if it isn't already.

Installing Rack'em

$ composer require rackem/rackem:@stable

Installing globally is awesome too:

$ composer global require rackem/rackem:@stable

Optionally, download Rack'em and require rackem.php:

require 'rackem/rackem.php';


rackem is a HTTP server for running Rack'em applications. This makes developing PHP applications a breeze.

Provide rackem your main application script, and you are good to go:

$ rackem config.php
== Rack'em on
>> Rack'em web server
>> Listening on, CTRL+C to stop


Anything that is_callable() or has an instance method call() can be considered an application. The application must return an HTTP response array containing: status code, headers, and body.

Here is an example of a basic Rack'em application:


class App
    public function call($env)
        return array(200,array('Content-Type'=>'text/html'),array('Hello World!'));

return \Rackem::run("App");

Rackem::run() accepts 1 of 3 things:

  • String referencing a Class
  • Class instance
  • Closure

Here would be an example of using a Closure:

$app = function($env) {
    return array(200,array('Content-Type'=>'text/html'),array('Hello World!'));
return \Rackem::run($app);


Fill your rack with middleware for ultimate awesomeness.

Middleware is basically an application that is passed the previous application in the stack and optionally an array of options in its constructor.

The most basic middleware (hint: it doesn't do anything):


class MyMiddleware
    public $app, $options;

    public function __construct($app, $options = array())
        $this->app = $app;
        $this->options = $options;

    public function call($env)
        return $this->app->call($env);

return \Rackem::run( new App() );

There is also of course a helper class to make things a bit easier:


class MyMiddleware extends \Rackem\Middleware
    public function call($env)
        // do stuff
        return parent::call($env);


You can route paths to applications easily:


\Rackem::map("/hello", function($env) {
    return array(200, array("Content-Type"=>"text/html"), array("Hello from Rack'em!"));


return \Rackem::run();

Request and Response


class JsonFormatter extends \Rackem\Middleware
    public function call($env)
        $req = new \Rackem\Request($env);
        $res = new \Rackem\Response($this->app->call($env));

        if($req->params()->format == 'json')    //?format=json
            $res[] = json_encode($res->body);
        return $res->finish();


Accepting pull requests for features and fixes!

Things that are in need:

  • more code coverage
  • web server test bed
  • benchmarking

Thank You