php ham路由库源码,Ham: php路由,简化自James Cleveland的Ham

Ham是一个轻量级的PHP微框架,专注于简洁的路由语法和高速缓存。它利用XCache或APC进行缓存,提高性能,减少重复的磁盘使用和路径搜索。框架鼓励使用PHP原生功能,并提供了应用嵌套、错误处理等功能,旨在简化PHP开发,提高代码可读性和可维护性。
摘要由CSDN通过智能技术生成

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.

Usage (v0.2)

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

route('/', 'hello');

route('/blog//', 'BlogView', array('get', 'post'));

run();

function hello($username='')

{

echo 'Hello ' . $username;

}

class BlogView extends View

{

public $blogs;

public function prepare()

{

$this->blogs = array();

}

public function get($id=0)

{

$blog = isset($this->blogs[$id]) ? $this->blogs[$id] : null;

return $blog;

}

}

Requirements (v0.1)

PHP 5.3

XCache (preferred) or APC (still optional)

Requests pointed at file that you put the app in (eg.

index.php).

Hello World (v0.1)

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

$app = new Ham('example');

$app->route('/', function($app) {

return 'Hello, world!';

});

$app->run();

More Interesting Example (v0.1)

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/', $hello);

$app->route('/', $hello);

$app->run();

Multiple apps mounted on routes! (v0.1)

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

Output:

/beans/

Beans home.

/beans/baked

Yum!

/

App home.

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值