PHP code example of aheinze / lime

1. Go to this page and download the library: Download aheinze/lime library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

aheinze / lime example snippets


$app = new Lime\App();

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

$app->run();

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

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

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

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

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

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

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

<p>
    Hello <?=$name

<!DOCTYPE HTML>
<html lang="en-US">
<head>
        <meta charset="UTF-8">
        <title><?=$title

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

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

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

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

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

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

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

class MyHelperClass extends Lime\Helper {

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

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

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

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

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