designcise / bitframe
BitFrame PHP microframework
Requires
- php: >=8.2
- ext-json: *
- psr/container: ^2.0
- psr/http-factory: ^1.0
- psr/http-message: ^2.0
- psr/http-server-middleware: ^1.0
Requires (Dev)
- mockery/mockery: ^1.3
- nyholm/psr7: ^1.8
- phpmd/phpmd: @stable
- phpstan/phpstan: *
- phpunit/phpunit: ^9.5
- squizlabs/php_codesniffer: 3.*
Suggests
- ext-fileinfo: For supporting embedded files when using BitFrame\Http\Message\FileResponse
- ext-libxml: For out-of-the-box XML support, please install libxml extension
- ext-simplexml: For out-of-the-box XML support, please install simplexml extension
- designcise/bitframe-fastroute: Add routing support to your App
- nyholm/psr7: Add PSR-7 / PSR-17 based HTTP Messaging support for your App
README
BitFrame PHP Microframework
A highly customizable PSR-15 / PSR-7 compatible middleware-based microframework for PHP that comes bundled with a simple PSR-11 based DI container for sharing services and data across the application. It is:
- Easy-to-learn and intuitive;
- Standards-based;
- Simple by design;
- Free of unnecessary bloat;
- Non-intrusive;
- Customizable, modular and easy-to-scale.
Prerequisites
- PHP 8.2+;
- Server with URL Rewriting (such as Apache, Nginx, etc.).
How to Get Started?
You can get started in a few simple steps:
- Setup your environment;
- Install
composer
dependencies; - Create your first "Hello World" app.
For a complete example, have a look at the simple dockerized boilerplate.
1. Setup Your Environment
You can refer to the following minimal Apache and Nginx configurations to get started:
Apache
Create an .htaccess
file with at least the following code:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [QSA,L]
This sets the directive in Apache to redirect all Http requests to a front controller index.php
file in which you can write your main application code.
Nginx
A configuration like the following in Nginx will help you set the directive to rewrite path to your application front controller (i.e. index.php
):
server {
listen 80;
server_name 127.0.0.1;
root /var/www/html/public;
index index.php;
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass app:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
}
Remember to make changes according to your project setup. For example, ensure that listen
, root
, fastcgi_pass
, *_log
, etc. are setup correctly according to your project.
2. Install Composer Dependencies
You can use composer require
like so:
$ composer require "designcise/bitframe":^4.0
Or, alternatively, you can add the package dependency in your composer.json
file.
Please note that you must include a PSR-17 factory in your composer dependencies. nyholm/psr7 and guzzle/psr7 are good examples of these — if you include either of these, they're automatically picked up by BitFrame. For any other PSR-17 factory implementation, you can add it add via \BitFrame\Factory\HttpFactory::addFactory()
method before you instantiate \BitFrame\App
.
3. Create Your First "Hello World" App
If you have ever used a middleware based framework, you will feel at ease. A "Hello World" app would look something like the following:
<?php require 'vendor/autoload.php'; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Server\RequestHandlerInterface; use BitFrame\App; use BitFrame\Emitter\SapiEmitter; // 1. Instantiate the App $app = new App(); $middleware = function (ServerRequestInterface $request, RequestHandlerInterface $handler) { $response = $handler->handle($request); $response->getBody()->write('Hello World!'); return $response; }; // 2. Add middleware $app->use([ SapiEmitter::class, $middleware, ]); // 3. Run the application $app->run();
From the code above you can see that the application uses two middlewares:
- A PSR-15 middleware
\BitFrame\Emitter\SapiEmitter
(that comes bundled with BitFrame package) which allows to emit the HTTP Response to the requesting user-agent (such as a web browser); - A closure middleware used to write
Hello World!
to the HTTP response stream.
This is of course a very basic example. In a real-world application, the setup would be much more complex than this. For example, you could extend the functionality by using an additional PSR-15 middleware such as a router, error handler, etc. For suggestions on how to go about designing your application (and to get started quickly), have a look at the simple dockerized boilerplate example.
Tests
To run the tests you can use the following commands:
Command | Type |
---|---|
composer test |
PHPUnit tests |
composer style |
CodeSniffer |
composer style-fix |
CodeSniffer Fixer |
composer md |
MessDetector |
composer check |
PHPStan |
Contributing
Please see CONTRIBUTING for details.
- File issues at https://github.com/designcise/bitframe/issues
- Issue patches to https://github.com/designcise/bitframe/pulls
License
Please see License File for licensing information.