balintsera/exphpress

A react php clone of the famous Express framework.

dev-master 2020-03-17 08:12 UTC

This package is auto-updated.

Last update: 2024-04-17 17:35:15 UTC


README

Exphpress (pronounced as 'exfress') is a PHP clone of the famous Node.js framework, Express. The goal is a 80% api compatibility with it.

It's just a hobby project for fun, but it can be useful if you need your well-known Express.js and async scalability.

It's already faster then a default Laravel install by more than 9000 times.

Yes, it's more than 9000 times. 🚀🚀🚀🚀

The reason is simple: while Laravel and every other frameworks by the way die after every request, Exphpress runs continuously and doesn't need to recompile and rerun everything when a request comes in.

And it's only 4 times slower then Express.js and I'll make everything that's possible to gain upon it.

Supported API calls

Node.js:

const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => res.send('Hello World!'))

app.use(function (req, res, next) {
  console.log('Example middleware')
  next()
})

app.listen(port, () => console.log(`Example app listening on port ${port}!`))

and the same in PHP:

$app = new \Exphpress\App();
$port = 8080;

$app->get('/hello', function (ServerRequestInterface $request) {
    return new Response(
        200,
        array(
            'Content-Type' => 'text/plain'
        ),
        'Hello World!'
    );
});

$app->use(function (ServerRequestInterface $request, callable $next) {
    error_log("example middleware");
    return $next($request);
});

$app->listen($port);

Run / Install

composer require balintsera/exphpress

Then organize your project the way you want, all the framework files are located in this package.

Because it has some special needs, like libuv and a pecl extension that wraps libuv, I recommend to use this dockerfile to build and run the example above.

FROM php:7.4-cli
COPY . /usr/src/app
WORKDIR /usr/src/app
RUN apt-get update \
    && apt-get install -y libuv1-dev

RUN pecl install uv-0.2.4 \
    && docker-php-ext-enable uv

# install and run composer 
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
RUN composer install --no-dev

CMD [ "php", "./app.php" ]

You can utilize this for development too with docker-compose:

version: "3"
services:
  exphpress:
    build: .
    ports: 
      - 8080:8080
    volumes:
    - ./app.php:/usr/src/app/app.php 

If you share the source of your app as volumes, you won't need to rebuild the image on every change (but the container still needs to be restarted).

All credits goes to the people who wrote reactPHP and all the other dependencies. Kudos.