Git Product home page Git Product logo

mu's Introduction

The µ PHP Microframework

Code Climate

A "real" microframework that fits in just 4 lines of code.

The microframeworks out there weren't micro enough for me, so I brushed up on some of my code golfing skills to create µ.

Features

These 4 LOC come jam-packed with features!

Easy, regex-based routing system

Follows the well-established route-to-callable microframework pattern.

echo (new µ)
    ->get('/hello', function ($app) {
        return "<p>Hello, world!</p>";
    })
    ->run($_SERVER['REQUEST_METHOD'], $_SERVER['REQUEST_URI']);

Allows you to access parameters from the URL.

echo (new µ)
    ->get('/hello/(?<name>\w+)', function ($app, $params) {
        return "<p>Hello, {$params['name']}!</p>";
    })
    ->run($_SERVER['REQUEST_METHOD'], $_SERVER['REQUEST_URI']);

Supports all your favorite HTTP verbs.

echo (new µ)
    ->delete('/user/(?<id>\d+)', $fn)
    ->get('/user/(?<id>\d+)', $fn)
    ->head('/user/(?<id>\d+)', $fn)
    ->patch('/user/(?<id>\d+)', $fn)
    ->post('/users', $fn)
    ->put('/user/(?<id>\d+)', $fn)
    ->run($_SERVER['REQUEST_METHOD'], $_SERVER['REQUEST_URI']);

Supports wildcard verbs too, because sometimes you are just making a web page and you really don't care about esoteric HTTP practices.

echo (new µ)
    ->any('/', $fn)
    ->run($_SERVER['REQUEST_METHOD'], $_SERVER['REQUEST_URI']);

Simple, but powerful, dependency injection container

use Monolog\Logger;
use Monolog\Handler\StreamHandler;

echo (new µ)
    ->cfg('log.channel', 'your-app')
    ->cfg('log.handler', function ($app) {
        return new StreamHandler('path/to/your.log', Logger::WARNING);
    })
    ->cfg('log', function ($app) {
        $log = new Logger($app->cfg('log.channel'));
        $log->pushHandler($app->cfg('log.handler'));
        return $log;
    })
    ->get('/hello/(?<name>\w+)', function ($app, $params) {
        $app->cfg('log')->addDebug("Said hello to {$params['name']}.");
        return "<p>Hello, {$params['name']}!</p>";
    })
    ->run($_SERVER['REQUEST_METHOD'], $_SERVER['REQUEST_URI']);

A truly elegant and fluent interface

See previous example (I'm lazy).

Built-in templating system, free of {}

Templates are just PHP files—no mustaches and no frills.

<!-- templates/hello.php -->
<html>
  <head>
    <title>World Greeter</title>
  </head>
  <body>
    <p><?= ucfirst($greeting) ?>, <?= $name ?>!</p>
  </body>
</html>
// index.php
echo (new µ)
    ->cfg('views', __DIR__ . '/templates')
    ->any('/hello/(?<name>\w+)', function ($app, $params) {
        return $app->view('hello', [
            'greeting' => 'howdy',
            'name'     => $params['name'],
        ]);
    })
    ->run($_SERVER['REQUEST_METHOD'], $_SERVER['REQUEST_URI']);

No twigs, plates, or blades to cut you or poke you.

Design constraints

  • Must have at least a Router, Container, and Templating System as features.
  • Must attempt to incorporate usage patterns (e.g., chainable methods, closures as controllers) that resemble other contemporary microframeworks.
  • Must work with error_reporting set to -1 (all errors reported).
  • Must not exceed 4 lines of code (LOC), where each line is <= 120 characters.
  • Must not have dependencies on other packages.
  • May break traditional coding conventions for the sake of brevity.
  • Must be hand-written.

It works, but it's really just a joke.

Don't use this in production, or really anywhere. It's just for fun. 😄

If you want to use a production-quality microframework, try one of these:

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.