Skip to content
davenusbaumPublic

About

PHP Inspired by Node Express

Resources

Stars

3 stars

Watchers

2 watching

Forks

Latest commit

 

History

47 Commits

Folders and files

Repository files navigation

Pine

A minimalist PHP web framework inspired by Express.js.

The world probably doesn't need another PHP framework — but I needed one that felt like Express: short, concise, and out of your way.

Installation

composer require davenusbaum/pine

Quick Start

<?php

use Pine\Application;

require 'vendor/autoload.php';

$app = new Application();

$app->router->get('/', function ($req, $res) {
    echo 'Hello World';
});

$app->handle($_SERVER, $_GET, $_POST, $_COOKIE);

Routing

$app->router->get('/users/:id', function ($req, $res) {
    $res->json(['id' => $req->params->id]);
});

$app->router->post('/users', function ($req, $res) {
    $name = $req->body->get('name');
    // ...
});

Handlers can be closures, invokable classes, or [ClassName, 'method'] pairs:

$app->router->get('/users/:id', [UserController::class, 'show']);
$app->router->post('/users', UserController::class);

Multiple handlers on the same route are registered separately:

$app->router->get('/users', 'Auth');
$app->router->get('/users', 'UserList');

Groups

Routes sharing a path prefix can be grouped together:

$app->router->group('/api', function ($router) {
    $router->get('/users', 'UserList');
    $router->get('/users/:id', [UserController::class, 'show']);
    $router->post('/users', UserController::class);
    $router->patch('/users/:id', [UserController::class, 'update']);
    $router->delete('/users/:id', [UserController::class, 'destroy']);
});

Middleware

Middleware uses PHP generators. Code before yield runs on the way in; code after yield runs on the way out — making before/after patterns natural:

$app->router->use('/api', function ($req, $res) {
    if (!$req->get('Authorization')) {
        $res->status(401);
        $res->end();
        return;
    }
    yield;
    header('X-Response-Time: ' . microtime(true));
});

$app->router->get('/api/users', function ($req, $res) {
    $res->json(['users' => []]);
});

Omit the path to run middleware on every route:

$app->router->use($logger);
$app->router->use($session);

Plain handlers without yield work fine too — the generator is opt-in.

Request

$req->params->id              // route parameters (:id)
$req->query->page             // query string (?page=2)
$req->body->get('email')      // POST field or JSON property
$req->body->get('user.email') // nested JSON via dot notation
$req->get('Accept')           // request header
$req->method                  // GET, POST, etc.
$req->path                    // /users/42
$req->ip                      // remote IP address

Response

$res->json(['status' => 'ok']);   // send JSON
$res->render('users/profile');    // render a template
$res->redirect('/login');         // redirect
$res->status(404)->end();         // set status and stop

Templates

Pine uses PHP templates by default. Configure the views path and call $res->render() with the template name (without extension):

$app->set('views', __DIR__ . '/views');

$app->router->get('/profile', function ($req, $res) {
    $res->locals->set('user', $user);
    $res->render('profile'); // renders views/profile.php
});

To use Twig instead:

use Pine\TwigEngine;

$app->set('render engine', new TwigEngine(__DIR__ . '/views'));

Session & Flash

use Pine\Middleware\Session;
use Pine\Middleware\Flash;

$session = new Session();
$flash = new Flash();

$app->router->use($session);
$app->router->use($flash);

$app->router->post('/login', function ($req, $res) {
    $req->session->set('user_id', 42);
    $res->locals->flashMessages->flashSuccess('Welcome back!');
    $res->redirect('/dashboard');
});

Flash messages survive a redirect automatically and are available in the next request's template via $flashMessages.

Precompiled Routes

The route tree is a plain PHP array and can be cached as an opcache-friendly include file, eliminating route registration overhead on every request:

// build once
$tree = $app->router->getRouteTree();
file_put_contents('routes.cache.php', '<?php return ' . var_export($tree, true) . ';');

// load on each request
$app = new Application(include 'routes.cache.php');

About

PHP Inspired by Node Express

Resources

Stars

3 stars

Watchers

2 watching

Forks

Releases

Packages

Used by

Contributors

Languages