A very light weight PHP library to handle routing, controller management, SQL + memcache, and logging.
require_once('pie/index.php');
In your index.php
file, you will require something similar to the following:
require_once('pie/index.php');
// set up loader
Loader::setRootPath('/path/to/my/application/');
// set up configurations
Config::set('console.filePath', '/path/to/my/application/logs/server.' . date('Ymd') . '.log');
Config::set('console.noClient', false);
Config::set('console.verbose', false);
Config::set('controllerPath', '/path/to/my/application/controller/');
// set up logger
Console::setup(Config::get('console.filePath'), Config::get('console.noClient'), Config::get('console.verbose'));
// set up and run router
$router = new Router();
$router->setTrailingSlash(true);
$router->setControllerPath(Config::get('controllerPath'));
// set URI prefix: The URI parser will assume that every URI starts with the given prefix
$router->setUriPrefix('mobile');
// add reroute
$router->addReroute('/', '/test/index');
// error handling reroute
$router->addErrorReroute(404, '/error/notfound');
// start the app
$router->run();
These classes will be included when you you pie
library.
A static class to handle include
.
Set a root path for Loader to use as the root path.
Static method to load a PHP source code.
Example:
Loader::get('my/awesome/php/lib.php');
Static method to return the root path for Loader.
A static class to handle setting and getting configuration values across the application.
Returns the value of configuration by its name set by .set()
.
A router class to handle routing.
A static method to redirect.
Example:
Router::redirect('/redirect/here/', 301);
Enable/Disable enforced trailing slash in URL.
Set controller directory path where all controllers are.
Adds a rerouting path.
Exmaple:
$router->addReroute('/', '/reroute/here/');
The above example will reroute /
to /reroute/here/
.
Assign a URI to a specific error code such as 404
.
$router->addErrorReroute(404, '/error/notfound/');
The above example will execute /error/notfound/
on every 404
error.
Registers a callback
function on specified URI given by $uri
.
The registered callback
will be called on every matching request.
NOTE 1: The callback
MUST return HTTP status code for an error.
If there is no error in the hook, you may return 200
or execute some functions.
Example:
$router->addReuqestHook('/test/index/', 'myMethod', 'myRequestHookHandlerClass');
class myRequestHookHandlerClass {
public static function myMethod($request, $response) {
// check session
if (/* no session */) {
return 403;
}
// there is a session
$response->redirect('/mypage/');
}
}
NOTE 2: The hooks that are added to controller
ONLY will be executed for all requests with the same controller
.
Example:
$router->addRequestHook('/example/', 'myHook');
// the above will be exected on:
/*
/example/
/example/index/
/example/boo/
/example/foo/
etc...
*/
A static class for logging both on server and client (browser).
Set up Console class.
If given, Console will be logging to the path given on the server.
If false
, Console will not be logging in console of browser.
Default value is false
.
If false
Console will not output log
, but warn
and error
only.
Default value is true
.
Returns an instance of ConsoleCore object for logging.
Example:
Console::init('/logging/path/', true);
$console = Console::create();
$console->log('boo');
$console->warn('foo');
$console->error('too');
NOTE: Console will catch uncaught exceptions and log the error automatically
pie
library handles each request by a cooresponding controller
.
For example a URL /example/index
will be executing a controller class in controller/example/index.class.php
.
First, you must set a controller path as shown below:
$router = new Router();
$router->setControllerPath('path/to/my/controller/');
You will then define a URI by creating a controller directory and a method file in it:
# Define controller 'example'
mkdir path/to/my/controller/example
# A controller method called index
path/to/my/controller/example/index.class.php
A controller must be a valid PHP class such as:
<?php
class Controller {
public function __construct() {
}
public function GET($request, $response, $params) {
}
}
The public method GET
will handle GET /example/index
.
The class name MUST be Controller
.
Each controller method will have 3 arguments:
$request
is an instance of a Request class.
Returns a matching request data of GET/POST/PUT/DELETE
Returns a map of all request data of GET/POST/PUT/DELETE
Returns a matching request header.
Returns all request headers.
Assigns variables to be used in response output.
If $source
is a path to a template file, it will load it and output its content.
If $source
is a string value, it will output as it is.
Outputs assigned variables (by .assign()) as a JSON string.
Redirects to the given $uri
with given status code
.
If status code
is not given, it will default to '301'.
An array of URL parameters.
Examaple:
/example/index/one/two/three
$params = array(
'one',
'two',
'three'
);
A static class to output "assigned" values on controllers to templates.
Returns a value assigned by $response->assign()
.
Example:
// in your controller
$response->assign('boo', 'Boo');
<!-- in your HTML template -->
<?= Data::get('boo') ?>
pie
library comes with a very simple SQL + Memcache class.
We need to properly setup DataSource
.
NOTE: DataSource
supports both mysql
and pgsql
.
// this creates a data model
DataSource::create('myModel');
// this is how you access the created data model anywhere in your application
$model = DataSource::get('myModel');
// ttl of cache to be 60000ms
$model->setupCache('localhost', 11211, 60000);
$model->setupMaster('mysql', 'localhost', 'myDBName', 'myUser', 'myPassword');
// typically slave would have different configurations than master
$model->setupSlave('mysql', 'localhost', 'myDBName', 'myUser', 'myPassword');
With DataSource
, we have data model
.
$myModel = DataSource::get('myModel');
Create a new instance of a data model to access SQL.
DataSource::create('myDataModel');
Return an instance of a data model created by ::create()
.
DataSource::create('myDataModel');
$model = DataSource::get('myDataModel');
This class is to handle SQL queries and memchache.
Setup memcache connection and TTL (it is in seconds).
Disable memcache.
Setup SQL master connection.
Setup SQL slave connection.
Returns the results of the query.
This method reads from slave
.
Memcache is used.
Returns the results of the query.
This method reads from master
.
Memcache is NOT used.
Executes the query on master
with auto-rollback on an exception error if in transaction
.
Updates memcache time.
Starts a transaction.
Commits transactioned queries.
Rolls back transactioned queries.
A static class to create a unique ID.
The ID is a string.
$uid = Uid::create();
Returns a unique ID string.
A static class to create a hash and validates.
Useful for secure password validation.
$pass = 'secret';
$hash = Encrypt::createHash($pass);
$validated = Encrypt::validateHash($pass, $hash);
Returns a hash of a given argument $password
.
Validates hash and a given argument $password
and returns a boolean.
A static class to handle session
$ttl
in seconds.
A static class to resiter and handle uncaught exceptions.