2017 © Pedro Peláez
 

library rackem

Rack for PHP

image

rackem/rackem

Rack for PHP

  • Wednesday, September 30, 2015
  • by tamagokun
  • Repository
  • 4 Watchers
  • 65 Stars
  • 835 Installations
  • PHP
  • 3 Dependents
  • 0 Suggesters
  • 3 Forks
  • 4 Open issues
  • 19 Versions
  • 0 % Grown

The README.md

Rack for PHP

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

Latest Stable Version, (*2)

<?php
# 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

, (*3)

Features

  • 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., (*4)

Installing Rack'em

$ composer require rackem/rackem:@stable

Installing globally is awesome too:, (*5)

$ composer global require rackem/rackem:@stable

Optionally, download Rack'em and require rackem.php:, (*6)

<?php
require 'rackem/rackem.php';

rackem

rackem is a HTTP server for running Rack'em applications. This makes developing PHP applications a breeze., (*7)

Provide rackem your main application script, and you are good to go:, (*8)

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

Usage

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., (*9)

Here is an example of a basic Rack'em application:, (*10)

<?php

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:, (*11)

  • String referencing a Class
  • Class instance
  • Closure

Here would be an example of using a Closure:, (*12)

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

Middleware

Fill your rack with middleware for ultimate awesomeness., (*13)

Middleware is basically an application that is passed the previous application in the stack and optionally an array of options in its constructor., (*14)

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

<?php

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);
    }
}

\Rackem::use_middleware("MyMiddleware");
return \Rackem::run( new App() );

There is also of course a helper class to make things a bit easier:, (*16)

<?php

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

Mapping

You can route paths to applications easily:, (*17)

<?php

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

\Rackem::map("/admin","MyAdminApp");

return \Rackem::run();

Request and Response

<?php

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();
    }
}

Contributing

Accepting pull requests for features and fixes!, (*18)

Things that are in need:, (*19)

  • more code coverage
  • web server test bed
  • benchmarking

Thank You

The Versions

30/09 2015

dev-master

9999999-dev http://github.com/tamagokun/rackem

Rack for PHP

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

middleware http interface webserver rack

10/07 2015

0.5.2

0.5.2.0 http://github.com/tamagokun/rackem

Rack for PHP

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

middleware http interface webserver rack

18/03 2015

0.5.1

0.5.1.0 http://github.com/tamagokun/rackem

Rack for PHP

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

middleware http interface webserver rack

15/11 2014

0.5

0.5.0.0 http://github.com/tamagokun/rackem

Rack for PHP

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

middleware http interface webserver rack

14/11 2014

0.4.13

0.4.13.0 http://github.com/tamagokun/rackem

Rack for PHP

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

middleware http interface webserver rack

14/11 2014

0.4.12

0.4.12.0 http://github.com/tamagokun/rackem

Rack for PHP

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

middleware http interface webserver rack

11/09 2014

0.4.11

0.4.11.0 http://github.com/tamagokun/rackem

Rack for PHP

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

middleware http interface webserver rack

01/04 2014

0.4.10

0.4.10.0 http://github.com/tamagokun/rackem

Rack for PHP

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

middleware http interface webserver rack

23/01 2014

0.4.9

0.4.9.0 http://github.com/tamagokun/rackem

Rack for PHP

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

middleware http interface webserver rack

27/11 2013

0.4.7

0.4.7.0 http://github.com/tamagokun/rackem

Rack for PHP

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

middleware http interface webserver rack

26/11 2013

0.4.6

0.4.6.0 http://github.com/tamagokun/rackem

Rack for PHP

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

middleware http interface webserver rack

20/11 2013

0.4.5

0.4.5.0 http://github.com/tamagokun/rackem

Rack for PHP

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

middleware http interface webserver rack

12/06 2013

0.4.2

0.4.2.0 http://github.com/tamagokun/rackem

Rack for PHP

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

middleware http interface webserver rack

05/06 2013

0.4.1

0.4.1.0 http://github.com/tamagokun/rackem

Rack for PHP

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

middleware http interface webserver rack

29/05 2013

0.4

0.4.0.0 http://github.com/tamagokun/rackem

Rack for PHP

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

middleware http interface webserver rack

11/01 2013

0.3

0.3.0.0 http://github.com/tamagokun/rackem

Rack for PHP

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

middleware http interface webserver rack

09/05 2012

v0.2

0.2.0.0 http://github.com/tamagokun/rackem

Rack for PHP

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

middleware http interface webserver rack

29/03 2012

v0.1

0.1.0.0 http://github.com/tamagokun/rackem

Rack for PHP

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

middleware http interface webserver rack

15/03 2012

v0.0.9

0.0.9.0 http://github.com/tamagokun/rackem

Rack for PHP

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

middleware http interface webserver rack