Bullet Response Types

There are many possible values you can return from a route handler in Bullet to produce a valid HTTP response. Most types can be either returned directly, or wrapped in the $app->response() helper for additional customization.

Strings

$app = new Bullet\App();
$app->path('/', function($request) use($app) {
    return "Hello World";
});
$app->path('/', function($request) use($app) {
    return $app->response(500, "Hello Error!");
});
 

Strings result in a 200 OK response with a body containing the returned string. If you want to return a quick string response with a different HTTP status code, use the $app->response() helper.

Booleans

$app = new Bullet\App();
$app->path('/', function($request) use($app) {
    return true;
});
$app->path('notfound', function($request) use($app) {
    return false;
});
 

Boolean false results in a 404 “Not Found” HTTP response, and boolean true results in a 200 “OK” HTTP response.

Integers

$app = new Bullet\App();
$app->path('teapot', function($request) use($app) {
    return 418;
});
 

Integers are mapped to their corresponding HTTP status code. In this example, a 418 “I’m a Teapot” HTTP response would be sent. This feature can be very useful for returning 404 errors if database records cannot be found, etc.

Arrays

$app = new Bullet\App();
$app->path('foo', function($request) use($app) {
    return array('foo' => 'bar');
});
$app->path('bar', function($request) use($app) {
    return $app->response(201, array('bar' => 'baz'));
});
 

Arrays are automatically passed through json_encode and the appropriate Content-Type: application/json HTTP response header is sent.

Templates

// Configure template path with constructor
$app = new Bullet\App(array(
    'template.cfg' => array('path' => __DIR__ . '/templates')
));
 
// Routes
$app->path('foo', function($request) use($app) {
    return $app->template('foo');
});
$app->path('bar', function($request) use($app) {
    return $app->template('bar', array('bar' => 'baz'))
        ->status(201);
});
 

The $app->template() helper returns an instance of Bullet\View\Template that is lazy-rendered on __toString when the HTTP response is sent. The first argument is a template name, and the second (optional) argument is an array of parameters to pass to the template for use.

Redirects

$app->path('foo', function($request) use($app) {
    return "Hello Foo!"
});
 
// 'bar' redirects to  'foo' with a 302 code
$app->path('bar', function($request) use($app) {
    return $app->response()->redirect('foo');
});
// 'baz' redirects to 'foo' with a 301 code
$app->path('baz', function($request) use($app) {
    return $app->response()->redirect('foo', 301);
});
 

Redirects result in a 302 Found HTTP response with a Location: header for the path provided. If you want to send a different status code, pass it as the second (optional) argument.

Fork me on GitHub