2017 © Pedro Peláez
 

library lime

The PHP micro-framework

image

aheinze/lime

The PHP micro-framework

  • Thursday, March 22, 2018
  • by aheinze
  • Repository
  • 24 Watchers
  • 165 Stars
  • 121 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 14 Forks
  • 8 Open issues
  • 2 Versions
  • 5 % Grown

The README.md

Lime

Lime is a micro web framework for quickly creating web applications in PHP with minimal effort., (*1)

$app = new Lime\App();

$app->bind("/", function() {
    return "Hello World!";
});

$app->run();

Just include one file (~ 35KB) and you're ready to go., (*2)

Routes

In Lime, a route is an HTTP method paired with a URL-matching pattern. Each route is associated with a block:, (*3)

$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., (*4)

Route patterns may include named parameters, accessible via the params hash:, (*5)

$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"]);
});

Conditions

Routes may include a variety of matching conditions, such as the user agent:, (*6)

$app->get("/foo", function() {
    // GET request...
}, strpos($_SERVER['HTTP_USER_AGENT'], "Safari")!==false);

Create Urls

$route = $app->routeUrl('/my/route');
$url   = $app->baseUrl('/assets/script.js');

Templates

In general you can utilize any template engine you want. Lime provides a simple template engine:, (*7)

$app->get("/", function() {

        $data = array(
            "name"  => 'Frank',
            "title" => 'Template demo'
        );

        return $this->render("views/view.php with views/layout.php", $data);
});

views/view.php:, (*8)

<p>
    Hello <?=$name?>.
</p>

views/layout.php:, (*9)

<!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>

You like OO style?

Just bind a class:, (*10)

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

Registry

Store any values by setting a key to the $app object., (*11)

$app["config.mykey"] = array('test' => 123);

Path access helper with /, (*12)

$value = $app["config.mykey/test"]; // will return 123

Paths

Register paths for quicker access, (*13)

$app->path('views', __DIR__.'/views');

$view = $app->path('views:detail.php');
$view = $app->render('views:detail.php');

Gets url to file, (*14)

$url  = $app->pathToUrl('folder/file.php');
$url  = $app->pathToUrl('view:file.php');

Dependency injection

$app->service("db", function(){

    // object will be lazy created after accessing $app['db']
    $obj = new PDO(...);

    return $obj;

});

$app["db"]->query(...);

Events


// 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, (*15)

// 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;
    }
});

Helpers

You can extend Lime by using your custom helpers:, (*16)

class MyHelperClass extends Lime\Helper {

    public function test(){
        echo "Hello!";
    }
}

$app->helpers["myhelper"] = 'MyHelperClass';

$app("myhelper")->test();

Build in helpers

Session, (*17)

$app("session")->init($sessionname=null);
$app("session")->write($key, $value);
$app("session")->read($key, $default=null);
$app("session")->delete($key);
$app("session")->destroy();

Cache, (*18)

$app("cache")->setCachePath($path);
$app("cache")->write($key, $value, $duration = -1);
$app("cache")->read($key, $default=null);
$app("cache")->delete($key);
$app("cache")->clear();

The Versions

22/03 2018

dev-master

9999999-dev https://github.com/aheinze/Lime

The PHP micro-framework

  Sources   Download

MIT

The Requires

  • php >=5.6.0

 

php microframework micro-framework lime

28/08 2015

1.0.0

1.0.0.0 https://github.com/aheinze/Lime

The PHP micro-framework

  Sources   Download

MIT

The Requires

  • php >=5.4.2

 

php microframework micro-framework lime