bittyphp/bitty

A tiny and simple PHP framework. No fuss, no muss, no coconuts.

v0.2.0 2019-03-03 23:21 UTC

README

Build Status Codacy Badge PHPStan Enabled Mutation Score Total Downloads License

A tiny and simple PHP framework. No fuss, no muss, no coconuts.

Work in Progress

As a warning, this is a work in progress. Things may break randomly. Use at your own risk.

Purpose

Bitty began as a learning experiment, but evolved into the desire to build something that followed PSRs without adding too many non-standard additions. Several libraries have PSR-compliant implementations, but they add extra methods and unwanted dependencies. I wanted something without the bloat - something that was "itty bitty" (hence the name Bitty).

Installation

It's best to install Bitty using Composer.

$ composer require bittyphp/bitty

Since Bitty doesn't force what view to use or what kind of controller you use, you may or may not want to also require the following:

# An abstract controller with common methods added for convenience
$ composer require bittyphp/controller

# A view based on the Twig engine
$ composer require bittyphp/view-twig

There's view layers for Twig, Mustache, Latte, and Plates. If those aren't enough, you can also make your own.

Setup

Starting with Bitty is easy. The main application has shortcuts for adding routes, managing middleware, accessing the container, and registering services.

Adding Routes

There are helper methods for adding routes for get, post, put, patch, delete, options, and a generic map for supporting multiple methods on the same route.

See Bitty's Router docs for more details.

<?php

require(dirname(__DIR__).'/vendor/autoload.php');

use Bitty\Application;
use Bitty\Http\Response;
use Psr\Http\Message\ServerRequestInterface;

$app = new Application();

$app->get('/', function (ServerRequestInterface $request) {
    return new Response('Hello, world!');
});

$app->patch('/foo', function (ServerRequestInterface $request) {
    return new Response('PATCHed /foo');
});

$app->map(['GET', 'POST'], '/foo', function (ServerRequestInterface $request) {
    return new Response('I support GET and POST');
});

$app->run();

Managing Middleware

Bitty supports any PSR-15 middleware. See the Middleware docs for more info.

<?php

require(dirname(__DIR__).'/vendor/autoload.php');

use Bitty\Application;
use Psr\Http\Server\MiddlewareInterface;

$app = new Application();

/** @var MiddlewareInterface */
$myMiddleware = ...;

$app->add($myMiddleware);

$app->run();

Accessing the Container

Bitty comes with a PSR-11 container. See the Container docs for how you can manage it.

<?php

require(dirname(__DIR__).'/vendor/autoload.php');

use Bitty\Application;

$app = new Application();

$container = $app->getContainer();

$app->run();

Registering Services

You can easily register services with the container using any service provider implementation.

<?php

require(dirname(__DIR__).'/vendor/autoload.php');

use Bitty\Application;
use Interop\Container\ServiceProviderInterface;

$app = new Application();

/** @var ServiceProviderInterface */
$myProvider = ...;

$app->register($myProvider);

$app->run();

Standards

Bitty adheres the following framework standards:

It also follows (or closely follows) some proposed standards:

Lacking

Bitty does not have built-in support for the following. At least not yet.

Credits

Bitty follows some design ideas from Symfony, specifically in the realm of security. The main application follows a similar design as Slim and Silex (and possibly others).