• Stars
    star
    280
  • Rank 147,053 (Top 3 %)
  • Language
    PHP
  • License
    MIT License
  • Created over 9 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

The 3-LOC µ (mu) PHP Micro-framework. Just cuz.

The µ PHP Micro-framework

A "real" :trollface: micro-framework that fits in just 3 lines of code.

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

Check out the code!

Where "line of code" means "as much code as possible crammed into <= 120 characters".

Features

These 3 LOC come jam-packed with features!

Easy, regex-based routing system

Follows the well-established route-to-callable micro-framework pattern.

(new µ)
    ->get('/hello', function () {
        echo "<p>Hello, world!</p>";
    })
    ->run();

Allows you to access parameters from the URL.

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

Supports all your favorite HTTP verbs.

(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();

Simple dependency/config container

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

(new µ)
    ->cfg('log.channel', 'your-app')
    ->cfg('log.handler', function () {
        return new StreamHandler('path/to/your.log', Logger::DEBUG);
    })
    ->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')->debug("Said hello to {$params['name']}");
        echo "<p>Hello, {$params['name']}!</p>";
    })
    ->run();

If a callable is provided (like with log.handler above), then it is treated as a factory and is only called once to produce a singleton value for efficient, multiple accesses.

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
(new µ)
    ->get('/hello/(?<name>\w+)', function ($app, $params) {
        echo $app->view(__DIR__ . '/templates', 'hello', [
            'greeting' => 'howdy',
            'name'     => $params['name'],
        ]);
    })
    ->run();

No Twigs, Plates, or Blades to cut you or poke you. That might feel a little dull, but it's simple.

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 micro-frameworks.
  • Must work with error_reporting set to -1 (all errors reported).
  • Must not exceed 3 lines of code (LOC), where each line is <= 120 characters.
  • Must not have dependencies on other packages.
  • May break traditional coding conventions/styles for the sake of brevity.
  • Must be hand-written, not minified/obfuscated by any tools.

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 micro-framework, try Slim.

Examples

The code examples in this README are also shipped as working examples in the /examples directory.

To run an example, use the built-in PHP server.

# For the hello1 example:
php -S localhost:8000 examples/hello1.php

Then access http://localhost:8000 in your browser or via cURL.

Tests

A very basic test suite is included, and can be run via:

php test.php

More Repositories

1

super_closure

Serialize closures. Not maintained. Consider using opis/closure.
PHP
1,719
star
2

xstatic

Static Proxies (like Laravel "Facades") in any PHP project
PHP
104
star
3

php-design-patterns

Design patterns implemented using traits in PHP 5.4
PHP
101
star
4

iter8

PHP library for iterable/generator transformations and operations
PHP
49
star
5

FunctionParser

Parses PHP functions/methods/closures to get the code
PHP
49
star
6

JeoPHPardy

A Jeopardy-like game and score board for hosting PHP trivia games.
PHP
45
star
7

slack-block-kit

DEPRECATED: Use https://github.com/slack-php/slack-php-block-kit instead
PHP
30
star
8

php-func-mocker

[DEPRECATED] Overwrite/mock PHP functions from the global namespace used in another namespace for the purposes of testing.
PHP
19
star
9

FunnyFacesL4ExampleApp

Example Laravel application using the AWS SDK for PHP
PHP
18
star
10

microlib-config

A small, PHP, configuration library consisting mostly of functions.
PHP
15
star
11

tweetmvc-core

TweetMVC [DEPRECATED] - See jeremeamia/mu
PHP
13
star
12

kohana-less

A Ko3 module for easy inclusion and use of LESS
PHP
11
star
13

sunshinephp-guzzle-examples

Example code for my SunshinePHP Guzzle Tutorial
PHP
10
star
14

phacture

Building PHP objects since 2013.
PHP
10
star
15

recursed

Using recursion to look for recursion in PHP code.
PHP
9
star
16

Add-Another

A jQuery plugin that grants the ability for duplicating HTML content. This is useful for constructing repeated form elements like a multiple file uploader.
PHP
9
star
17

guzzle-swagger-lite

A "lite" Guzzle-based Swagger client that works with JSON services.
PHP
8
star
18

functions.php

Collection of interesting PHP functions
PHP
5
star
19

DesertCodeCampPHPMVC

A sample project for the PHP MVC presentation at the April 2011 Desert Code Camp
PHP
3
star
20

TraitsLoggerDemo

TraitsLogger
PHP
3
star
21

ASU-CS-Sample-MovieLibrary

Sample Project showing Unit Testing for an ASU CSE class
PHP
3
star
22

Blog-Demo-for-CSE-294

This was built from scratch as a sample project for the ASU CSE 294 class
PHP
2
star
23

acclimate-api

[Deprecated] Please use https://github.com/jeremeamia/acclimate-container
PHP
2
star
24

Presentation-for-CSE-294

This project contains my powerpoint and code samples for my presentation
PHP
1
star
25

azphp-s3-upload-demo

Demo code for presentation about uploading to S3 from PHP at azPHP
PHP
1
star