Getting Started
The easiest way to get up and running is by installing Composer. Go ahead and install it now if you haven't already.
Once composer is installed, you can require rackem/rackem
as a dependency of your project.
$ composer require rackem/rackem:@stable
Installing Rack'em globally is also pretty cool:
$ composer global require rackem/rackem:@stable
If you have installed rackem globally, you can serve Rack'em applications as simply as rackem
Supported web servers
- nginx (php-fpm)
- Apache
- PHP built-in server
- Ruby web servers
Your first Rack'em application
The best way to explain how Rack'em works is to dive right in with a tutorial.
Create a project
Create a project and require rackem/rackem
$ mkdir tutorial
$ cd tutorial
$ composer require rackem/rackem:@stable
$ composer install
Create an application
A Rack'em application is any object that reponds to the method call()
or a function closure. The rackem utility by default will look for config.php
as your main application script, so we will create one now.
$ touch config.php
<?php
# config.php
return \Rackem::run(function($env) {
return array(
200,
array("Content-Type"=>"text/html"),
array("<h1>Hello, from Rack'em!</h1>")
);
});
A Rack'em application must return an array containing the HTTP response status, headers, and body. In our example application you can see that we are specifying a 200 response code, setting the Content Type to text/html, and supplying a short html greeting.
Run it
Once you have your config.php saved, all you need to do is run the rackem utility, and your first application is running!
$ vendor/bin/rackem
== Rack'em on http://0.0.0.0:9393
>> Rack'em web server
>> Listening on 0.0.0.0:9393, CTRL+C to stop
Browse to http://localhost:9393 to see your application in action.
rackem
The rackem utility is a tiny web server that can serve up any Rack'em application. Though it is pretty fast when used developing locally, I would not recommend using it as a production web server.
When running the server, it will by default look for your main application script in config.php, you can alternatively specify the name of your main application script.
$ vendor/bin/rackem index.php
By default rackem will run on localhost with port 9393. Both host and port are configurable:
$ vendor/bin/rackem index.php --host dev.local --port 3000
If you pass --basic
rackem will give you a basic html web server for serving up static websites.
$ vendor/bin/rackem --basic
Class Reference
Rackem
Probably the class you are looking for. Provides helper functions for quickly creating and managing application stacks. Utilizes a single Rackem\Builder
instance.
-
\Rackem::map($path, $app)
Map an application to a url path.
Example:
<?php \Rackem::map("/admin", $admin_app); return \Rackem::run($app);
-
\Rackem::use_middleware($middleware, $options = array())
Adds a middleware to the stack. You can pass in a string reference to a class with an options array, or an instantiated middleware.
-
\Rackem::run($app)
Dispatch an application to be handled by a web server. You must
return
this call in order for Rack'em to properly handle things. -
\Rackem::version()
Returns an array of the current Rack'em version.
Rackem\Builder
Builder is the bread and butter of creating a Rack'em application stack.
-
__construct($app = null, $middleware = array() )
Optionally pass in a default application and set of middleware when creating your instance.
-
->call($env)
Returns response of application stack as an array of status code, headers, and body.
-
->map($path, $app)
Map an application to a url path.
-
->run($app)
Sets
$app
as the main application. -
->use_middleware($middleware, $options = array())
Adds a middleware to the stack. You can pass in a string reference to a class with an options array, or an instantiated middleware.
Rackem\Exception
Halt an application with a specified response array.
Example:
<?php
public function call($env)
{
throw new \Rackem\Exception(404, array("text/plain"), array("Not found"));
}
-
__construct($status, $header, $body)
Creates an Exception with the specified http response array.
-
->finish()
Returns the response array.
Rackem\File
An application used for serving file requests through Rack'em. Uses $env['PATH_INFO']
to determine the path to the file.
Example:
<?php
public function call($env)
{
$file = new \Rackem\File(__DIR__);
return $file->call($env);
}
-
__construct($root_path, $cache_control = null)
File must live under
$root_path
or a 404 response is returned. Optionally specify a Cache-Control header value. -
->call($env)
On success will return the file contents as an http response array.
-
->serving($env)
Reads file and prepares http headers for response.
Rackem\Middleware
This is a base class used as a starting point when creating new middleware. Abstract class intended for extending.
-
__construct($app, $options = array())
middleware must accept the previous application in the stack in their constructor.
-
->call($env)
Returns response of previous application and middleware as an array of status code, headers, and body. This method must call the previous application using:
$this->app->call($env)
.
Rackem\Protection
A middleware that will help protect your application from common web exploits.
Protects against:
- Cross Site Request Forgery
- Cross Site Scripting
- Clickjacking
- Directory Traversal
- Session Hijacking
- IP Spoofing
-
\Rackem\Protection::protect($except = array(), $rackem = "\Rackem")
Hooks up protection middleware to your application in one line. Pass an array of protection middleware class names to exclude particular middlewares.
By default this method will use
\Rackem::use_middleware
, if you are using an instance ofRackem\Builder
to create your application stack, pass it in as the second paramter.
Rackem\Request
Easily get particular types of information from an http request.
Example:
<?php
public function call($env)
{
$req = new \Rackem\Request($env);
$req->base_url();
$req->path_info();
$req->content_type();
}
-
__construct($env = array())
Pass in a Rack'em environment object.
-
->base_url()
Returns the base url of the application.
Example: http://example.com
-
->body()
Returns the
rack.input
stream. -
->content_length()
Returns the content length of the request or
null
if there is none. -
->content_type()
Returns the content type of the request.
-
->cookies($key = null)
Returns an array of cookies from the request.
-
->form_data()
Returns
true
if the request method is POST. -
->fullpath()
Returns the full path of the request including the SCRIPT_NAME and QUERY_STRING.
Example: /app/some/request?foo=bar
-
->get()
Returns an array of all query string variables from the request.
Similar to
$_GET
. -
->host()
Returns the host name of the request.
Example: ripeworks.com
-
->host_with_port()
Returns the host name of the request with the port
Example: ripeworks.com:80
-
->is_delete()
Returns
true
if the request method is DELETE. -
->is_get()
Returns
true
if the request method is GET. -
->is_head()
Returns
true
if the request method is HEAD. -
->is_options()
Returns
true
if the request method is OPTIONS. -
->is_patch()
Returns
true
if the request method is PATCH. -
->is_post()
Returns
true
if the request method is POST. -
->is_put()
Returns
true
if the request method is PUT. -
->is_trace()
Returns
true
if the request method is TRACE. -
->is_xhr()
Returns
true
if the request is an AJAX request. -
->logger()
Returns the
rack.logger
stream object for logging. -
->media_type()
Returns the media type of the request if it exists.
Example:
-
->media_type_params()
Returns an array of any parameters from the media type.
-
->content_charset()
Returns the charset of the request if present.
-
->params()
Returns an array of all passed variables both GET and POST.
Similar to:
array_marge($_GET, $_POST)
-
->parseable_data()
Returns
true
if parseable data exists.Content type is of
multipart/related
ormultipart/mixed
-
->path()
Returns the path of the request including the SCRIPT_NAME.
-
->path_info()
Returns the path of the request.
Example: /css/site.css
-
->port()
Returns the port of the request.
-
->post()
Returns an array of POST variables from the request body.
Similar to
$_POST
-
->query_string()
Returns the query string of the request.
-
->referer()
Returns the request referer if present.
-
->request_method()
Returns the request method of the request.
Example: GET
-
->session($key = null)
Returns the session variable for
$key
if present, or an array of all session variables if$key
is null.Similar to:
$_SESSION
-
->scheme()
Returns the scheme of the request. http or https.
-
->ssl()
Returns
true
if the request is secured with SSL. -
->url()
Returns the full url of the request.
Example: http://ripeworks.com/css/site.css
-
->user_agent()
Returns the user agent of the request.
Rackem\Response
Used to easily formulate a response array. Will also attempt to normalize some headers like Content-Length.
Example:
<?php
// Application example
public function call($env)
{
$res = new \Rackem\Response(array(), 200, array("Content-Type"=>"text/html"));
$res[] = "<h1>Hello down there!</h1>";
return $res->finish();
}
// Middleware example
public function call($env)
{
$res = new \Rackem\Response($this->app->call($env));
$res[] = "Adding some content to the response...";
return $res->finish();
}
-
__construct($body = array(), $status = 200, $header = array())
You can directly pass in an http response array, or separately provide the body, status code, and headers.
-
[]
This class implements
ArrayAccess
, so you can use various array methods to alter the body of the response. -
->body(), __toString()
Returns the body of the response as a string.
-
->finish()
Returns the http response array.
-
->redirect($target, $status = 302)
Create the necessary response headers to perform a redirection.
-
->send($args)
Set various parts of the response based on the value of
$args
.Example:
<?php $res = new \Rackem\Response(); $res->send(200); // status code set to 200 $res->send(array("Hello, world!")); // "Hello, world!" written to body $res->send(array("Content-Type" => "text/plain")); // content-type header set to text/plain
-
->write($body)
Write a string or array to the response body.
-
->set_cookie($key, $value = array())
Set the cookie at
$key
to$value
. -
->delete_cookie($key, $value = array())
Delete the cookie at
$key
with value$value
.