PHP code example of anpv1 / iceage-php

1. Go to this page and download the library: Download anpv1/iceage-php 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/ */

    

anpv1 / iceage-php example snippets



// index.php
// composer autoload, install any dependencies you need
ction(){return 'Hello, world!';});
$result = $app->run();
$app->response($result);


// Routing with regex definition of parameters and multiple methods
// id in URL must be digit to match
$app->route(
    '/hello/:id|[0-9]+|', 
    '\\App\\Controller\\Hello::get', 
    'GET|POST'
);
// optional parameters
// this will match /blog/2017, /blog/2017/07, /blog/2017/07/01
$app->get('/blog(/:year|[\d]{4}|(/:month|[\d]{2}|(/:day|[\d]{2}|)?)?)?', function($route_params){
    return $route_params;
});

// grouping routes
$app->group('/admin', function(){
    $this->get('/photo', function(){
        return 'PhotoAdmin';
    }); // match /admin/photo

    $this->post('/gallery', function(){
        return 'GalleryAdmin';
    }); // match /admin/gallery

})->middleware(function($


// index.php
// composer autoload, install any dependencies you need
ter $db service
$app->register('db', function(){
    return new \PDO(
        $_ENV['DB_DSN'], 
        $_ENV['DB_USER'], 
        $_ENV['DB_PASSWORD'],
        array(
            \PDO::ATTR_PERSISTENT => true
        )
    );
});

// register Twig_Environment template
$app->register('Twig_Environment', function(){
    $loader = new \Twig_Loader_Filesystem(realpath('app/templates/views'));
    return new Twig($loader, array(
        'cache' => realpath('app/templates/cache'),
        'auto_reload' => true
    ));
});

// routes definition
// in the route handler you can use the $db service which is a PDO instance
// and load any parameter name which is a Twig_Environment instance
$app->get('/', function($db, Twig $template){
    return $template->render('template.html', array('message' => 'Hello, world!'));
});

$result = $app->run();
$app->response($result);



// public/index.php
chdir(getcwd().'/../');
// composer autoload, install any dependencies you need
Bootstrap\\Services::register',
    '\\App\\Bootstrap\\Routes::register'
));

try {
    $result = $app->run();
}
catch(Exception $e){
    $response = $app->run_handler('\\App\\Controller\\Error::error', array('error' => $e));
    $result = $app->response($response);
}

$app->response($result);



// app/Bootstrap/Env.php
namespace App\Bootstrap;
use Rfussien\Dotenv\Loader;

class Env {
    public static function load(){
        $dotenv = new Loader('app/');
        $dotenv->load();
    }
}



// app/Bootstrap/Routes.php
namespace App\Bootstrap;

class Routes {
    public static function register($app){
        // routes definition
        $app->get('/', '\\App\\Controller\\Index::get');
        $app->get('/login', '\\App\\Controller\\Login::index');
        $app->route(
            '/hello/:name/:id', 
            '\\App\\Controller\\Hello::get', 
            'GET|POST'
        );
        $app->get('/group/get/:id', '\\App\\Controller\\Group::get');
        $app->post('/user/signin', '\\App\\Controller\\Login::signin');
    }
}



// app/Bootstrap/Services.php
namespace App\Bootstrap;

class Services {
    private static $dbh;
    private static $twig;
    private static $acl;

    public static function register($app){
        $app->register('db', '\\App\\Bootstrap\\Services::db_service');
        $app->register('twig', '\\App\\Bootstrap\\Services::twig_service');
    }

    public static function db_service(){
        if(!self::$dbh){
            self::$dbh = new \PDO(
                $_ENV['DB_DSN'], 
                $_ENV['DB_USER'], 
                $_ENV['DB_PASSWORD'],
                array(
                    \PDO::ATTR_PERSISTENT => true
                )
            );
        }
        return self::$dbh;
    }

    public static function twig_service(){
        if(!self::$twig){
            $loader = new \Twig_Loader_Filesystem(realpath('app/templates/views'));
            self::$twig = new \Twig_Environment($loader, array(
                'cache' => realpath('app/templates/cache'),
                'auto_reload' => true
            ));
        }

        return self::$twig;
    }
}



// app/Controller/Hello.php
namespace App\Controller;

use App\DataTable\TestTbl;

class Hello
{
    // $route_params is a special service to get the parameters defined on route
    // In this case the route is /hello/:name/:id
    // So if the request URL is /hello/Bob/1 then $route_params['name'] = "Bob"
    // $route_params['id'] = 1
    public static function get($twig, $db, $route_params)
    {
        $test_tbl = new TestTbl($db);
        $row = $test_tbl->get($route_params['id']);
        return $twig->render('hello/index.html', array('row' => $row));
    }
}



// app/Controller/Error.php
namespace App\Controller;

class Error {
    // $error is passed from the below line in index.php
    // $response = $app->run_handler('\\App\\Controller\\Error::error', array('error' => $e));
    public static function error($twig, $error){
        return $twig->render('error/index.html', array('error' => $error, 'debug' => $_ENV['DEBUG_MODE']));
    }
}



$app->get('/', '\\App\\Controller\\Index::get')
    // middleware handler can use any registered services and params passed on
    ->middleware(function($db, $permission){
        // $permission = "admin"
        // implement middleware here
    },
    array('permission' => 'admin'))
    ->middleware(function($login_