micro php web framework
Lime is a micro web framework for quickly creating web applications in PHP with minimal effort.
$app = new Lime\App();
$app->bind("/", function() {
return "Hello World!";
});
$app->run();
Just include one file (~ 35KB) and you’re ready to go.
In Lime, a route is an HTTP method paired with a URL-matching pattern. Each route is associated with a block:
$app->get("/", function() {
return "This was a GET request...";
});
$app->post("/", function() {
return "This was a POST request...";
});
$app->bind("/", function() {
return "This was a GET or POST request...";
});
Routes are matched in the order they are defined. The first route that matches the request is invoked.
Route patterns may include named parameters, accessible via the params hash:
$app->get("/posts/:id/:name", function($params) {
return $params["id"].'-'.$params["name"];
});
$app->post("/misc/*", function($params) {
return $params[":splat"];
});
$app->bind("#/pages/(about|contact)#", function($params) {
return implode("\n", $params[":captures"]);
});
Routes may include a variety of matching conditions, such as the user agent:
$app->get("/foo", function() {
// GET request...
}, strpos($_SERVER['HTTP_USER_AGENT'], "Safari")!==false);
$route = $app->routeUrl('/my/route');
$url = $app->baseUrl('/assets/script.js');
In general you can utilize any template engine you want. Lime provides a simple template engine:
$app->get("/", function() {
$data = array(
"name" => 'Frank',
"title" => 'Template demo'
);
return $this->render("views/view.php with views/layout.php", $data);
});
views/view.php:
<p>
Hello <?=$name?>.
</p>
views/layout.php:
<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<title><?=$title?></title>
</head>
<body>
<a href="<?=$this->routeUrl('/')?>">Home</a>
<hr>
<?php echo $content_for_layout;?>
</body>
</html>
Just bind a class:
class Pages {
protected $app;
public function __construct($app){
$this->app = $app;
}
/*
accessible via
/pages or /pages/index
*/
public function index() {
return $this->app->render("pages/index.php");
}
/*
accessible via
/pages/contact
*/
public function contact() {
return $this->app->render("pages/contact.php");
}
/*
accessible via
/pages/welcome/foo
*/
public function welcome($name) {
return $this->app->render("pages/welcome.php", array("name"=>$name));
}
}
// bind Class to map routes
$app->bindClass("Pages");
Store any values by setting a key to the $app object.
$app["config.mykey"] = array('test' => 123);
Path access helper with /
$value = $app["config.mykey/test"]; // will return 123
Register paths for quicker access
$app->path('views', __DIR__.'/views');
$view = $app->path('views:detail.php');
$view = $app->render('views:detail.php');
Gets url to file
$url = $app->pathToUrl('folder/file.php');
$url = $app->pathToUrl('view:file.php');
$app->service("db", function(){
// object will be lazy created after accessing $app['db']
$obj = new PDO(...);
return $obj;
});
$app["db"]->query(...);
// register callback
$app->on("customevent", function(){
// code to execute on event
}, $priority = 0);
// trigger custom events
$app->trigger("customevent", $params=array());
You can utilize three system events: before, after and shutdown
// render custom error pages
$app->on("after", function() {
switch($this->response->status){
case "404":
$this->response->body = $this->render("views/404.php");
break;
case "500":
$this->response->body = $this->render("views/500.php");
break;
}
});
You can extend Lime by using your custom helpers:
class MyHelperClass extends Lime\Helper {
public function test(){
echo "Hello!";
}
}
$app->helpers["myhelper"] = 'MyHelperClass';
$app("myhelper")->test();
Session
$app("session")->init($sessionname=null);
$app("session")->write($key, $value);
$app("session")->read($key, $default=null);
$app("session")->delete($key);
$app("session")->destroy();
Cache
$app("cache")->setCachePath($path);
$app("cache")->write($key, $value, $duration = -1);
$app("cache")->read($key, $default=null);
$app("cache")->delete($key);
$app("cache")->clear();