A light weight router, memcache + SQL handling, and session management library.
A very light weight PHP library to handle routing, controller management, SQL + memcache, and logging., (*1)
require_once('pie/index.php');
In your index.php
file, you will require something similar to the following:, (*2)
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., (*3)
A static class to handle include
., (*4)
Set a root path for Loader to use as the root path., (*5)
Static method to load a PHP source code., (*6)
Example:, (*7)
Loader::get('my/awesome/php/lib.php');
Static method to return the root path for Loader., (*8)
A static class to handle setting and getting configuration values across the application., (*9)
Returns the value of configuration by its name set by .set()
., (*10)
A router class to handle routing., (*11)
A static method to redirect., (*12)
Example:, (*13)
Router::redirect('/redirect/here/', 301);
Enable/Disable enforced trailing slash in URL., (*14)
Set controller directory path where all controllers are., (*15)
Adds a rerouting path., (*16)
Exmaple:, (*17)
$router->addReroute('/', '/reroute/here/');
The above example will reroute /
to /reroute/here/
., (*18)
Assign a URI to a specific error code such as 404
., (*19)
$router->addErrorReroute(404, '/error/notfound/');
The above example will execute /error/notfound/
on every 404
error., (*20)
Registers a callback
function on specified URI given by $uri
., (*21)
The registered callback
will be called on every matching request., (*22)
NOTE 1: The callback
MUST return HTTP status code for an error., (*23)
If there is no error in the hook, you may return 200
or execute some functions., (*24)
Example:, (*25)
$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
., (*26)
Example:, (*27)
$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)., (*28)
Set up Console class., (*29)
If given, Console will be logging to the path given on the server., (*30)
If false
, Console will not be logging in console of browser., (*31)
Default value is false
., (*32)
If false
Console will not output log
, but warn
and error
only., (*33)
Default value is true
., (*34)
Returns an instance of ConsoleCore object for logging., (*35)
Example:, (*36)
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, (*37)
pie
library handles each request by a cooresponding controller
., (*38)
For example a URL /example/index
will be executing a controller class in controller/example/index.class.php
., (*39)
First, you must set a controller path as shown below:, (*40)
$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:, (*41)
# 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:, (*42)
getData($dataName [string]) Returns a matching request data of GET/POST/PUT/DELETE ###### ->getAllData() Returns a map of all request data of GET/POST/PUT/DELETE ###### ->getHeader($name [string]) Returns a matching request header. ###### ->getAllHeaders() Returns all request headers. ##### $response ###### ->assign($name [string], $value [mixed]) Assigns variables to be used in response output. ###### ->html($source [string], $statusCode [*number]) 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. ###### ->json($statusCode [*number]) Outputs assigned variables (by .assign()) as a JSON string. ###### ->redirect($uri [string], $statusCode [*number]) Redirects to the given `$uri` with given `status code`. If `status code` is not given, it will default to '301'. ##### $params An array of URL parameters. Examaple: ``` /example/index/one/two/three $params = array( 'one', 'two', 'three' ); ``` ## Data A static class to output "assigned" values on controllers to templates. #### Static Methods #### ::get($assignedName [string]) Returns a value assigned by `$response->assign()`. Example: ```php // in your controller $response->assign('boo', 'Boo'); ``` ```html = Data::get('boo') ?>
pie
library comes with a very simple SQL + Memcache class., (*43)
We need to properly setup DataSource
., (*44)
NOTE: DataSource
supports both mysql
and pgsql
., (*45)
// 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
., (*46)
$myModel = DataSource::get('myModel');
Create a new instance of a data model to access SQL., (*47)
DataSource::create('myDataModel');
Return an instance of a data model created by ::create()
., (*48)
DataSource::create('myDataModel'); $model = DataSource::get('myDataModel');
This class is to handle SQL queries and memchache., (*49)
Setup memcache connection and TTL (it is in seconds)., (*50)
Disable memcache., (*51)
Setup SQL master connection., (*52)
Setup SQL slave connection., (*53)
Returns the results of the query., (*54)
This method reads from slave
., (*55)
Memcache is used., (*56)
Returns the results of the query., (*57)
This method reads from master
., (*58)
Memcache is NOT used., (*59)
Executes the query on master
with auto-rollback on an exception error if in transaction
., (*60)
Updates memcache time., (*61)
Starts a transaction., (*62)
Commits transactioned queries., (*63)
Rolls back transactioned queries., (*64)
A static class to create a unique ID., (*65)
The ID is a string., (*66)
$uid = Uid::create();
Returns a unique ID string., (*67)
A static class to create a hash and validates., (*68)
Useful for secure password validation., (*69)
$pass = 'secret'; $hash = Encrypt::createHash($pass); $validated = Encrypt::validateHash($pass, $hash);
Returns a hash of a given argument $password
., (*70)
Validates hash and a given argument $password
and returns a boolean., (*71)
A static class to handle session, (*72)
$ttl
in seconds., (*73)
A static class to resiter and handle uncaught exceptions., (*74)