Code Monkey home page Code Monkey logo

ham's Introduction

Ham

Now includes tests!

PHP Microframework for use with whatever you like. Basically just a fast router with nice syntax, and a cache singleton. Will add more things as I go, like perhaps an extension system, autoloader and some other stuff to make developing in PHP less irritating than it currently is.

Routes are converted to regex and cached so this process does not need to happen every request. Furthermore, the resolved route for a given URI is also cached so on most requests thare is no regex matching involved.

There is also now the ability to mount apps on routes within apps, so one could make an administrator app, then mount it on the main app at /admin.

PHP presents an interesting challenge because due to it's architecture, everything has to be re-done each request, which is why I'm leveraging caching with tiny TTLs to share the results of operations like route resolution between requests.

Note: PHP already has many of the features that many microframeworks have, such as session handling, cookies, and templating. An aim of this project is to encourage the use of native functionality where possible or where it is good, but make some parts nicer or extend upon them to bring it up to scratch with the way I like things.

Note: For maximum speed gains, use the XCache extension because that supports caching of closures, unlike APC.

Goals

  • Make pretty much anything I/O related cached with XCache/APC (whichever is installed) in order to prevent excessive disk usage or path searching on lots of requests.
  • Provide a succinct syntax that means less magic and less code to read through and learn, without compromising speed or code length, by using native PHP methods and features.
  • Promote a simple, flat way of building applications that don't need massive levels of abstraction.
  • Encourage use of excellent third-party libraries such as Doctrine to prevent developers writing convoluted, unmaintainable code that people like me have to pick up and spend hours poring over just to get an idea of what on earth is going on.
  • Define and document development patterns that allow for new developers to get up to speed quickly and write new code that isn't hacky.

Inspired entirely by Flask.

Requirements

  • PHP 5.3
  • XCache (preferred) or APC (still optional)
  • Requests pointed at file that you put the app in (eg. index.php).

Hello World

require '../ham/ham.php';

$app = new Ham('example');

$app->route('/', function($app) {
    return 'Hello, world!';
});

$app->run();

More Interesting Example

require '../ham/ham.php';

$app = new Ham('example');
$app->config_from_file('settings.php');

$app->route('/pork', function($app) {
    return "Delicious pork.";
});

$hello = function($app, $name='world') {
    return $app->render('hello.html', array(
        'name' => $name
    ));
};
$app->route('/hello/<string>', $hello);
$app->route('/', $hello);

$app->run();

Multiple apps mounted on routes!

require '../ham/ham.php';

$beans = new Ham('beans');

$beans->route('/', function($app) {
    return "Beans home.";
});

$beans->route('/baked', function($app) {
    return "Yum!";
});

$app = new Ham('example');
$app->route('/', function($app) {
    return "App home.";
});
$app->route('/beans', $beans);
$app->run();

Custom Error Handeling

require 'ham/ham.php';

$beans = new Ham('beans');

$beans->route('/', function($app) {
    return "Beans home.";
});

$app->onError(function(){
    return "Burnt Bacon.";
}, "Error message can go here.");

$app->run();

Output:

/beans/

Beans home.

/beans/baked

Yum!

/

App home.

/definitely_not_the_page_you_were_looking_for

Burnt Bacon.

Have a gander at the example application for more details.

To-Dos

  • Nice logging class and logging support with error levels, e-mailing, etc.
  • Sub-application mounting (ala Flask "Blueprints").
  • Sanitisation solution.
  • CSRF tokens
  • Extension API

Extension Ideas

  • Form generation (3rd-party? Phorms)
  • ORM integration (most likely Doctrine)
  • Auth module (using scrypt or something)
  • Admin extension

ham's People

Contributors

jstacoder avatar robmiller avatar jeremykendall avatar gigablah avatar champloo11 avatar miramir avatar radiosilence avatar wogsland avatar thisisbrians avatar bronzehedwick avatar chrislaskey avatar

Watchers

Michael Sandritter avatar

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.