actions + routing + response

Installs: 8

Dependents: 0

Stars: 1

Watchers: 1

Forks: 0

Open Issues: 0

2.0.0 2016-01-30 09:03 UTC

README

  • actions + routing + response
  • requires PHP 5.6+

Here's how you'd use pico.

<?php

require 'path/to/pico.php';

use noodlehaus\pico;

```php
<?php

require 'path/to/pico.php';

use noodlehaus\pico;

# create a stack of actions
$routes = [
  pico\action('GET', '/books.json', function ($db, $config) {
    $list = loadAllBooks($db);
    $json = json_encode($list);
    return pico\response($json, 200, ['content-type' => 'application/json']);
  }),
  pico\action('GET', '/books/:id', function ($args, $db) {
    $title = loadBookNameById($db, $args['id']);
    return pico\response($title);
  }),
  pico\action('GET', '/about', page(__DIR__.'/views/about'))
];

# sample dependencies
$config = require __DIR__.'/config.php';
$db = createDBConnection($config['db']);

# we need the method and requested path
$verb = $_SERVER['REQUEST_METHOD'],
$path = $_SERVER['REQUEST_URI'],

# serve app against verb + path, pass dependencies
list($content, $status, $headers) = pico\serve($routes, $verb, $path, $db, $config);

# flush out content, status code, and http headers
pico\render($content, $status, $headers);

tests

Tests for pico use plain assertions.

php pico-tests.php

If no errors were echoed, then you're all good.

license

MIT