PHPackages                             pie/pie - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. pie/pie

ActiveLibrary

pie/pie
=======

A light weight router, memcache + SQL handling, and session management library.

012PHP

Since Oct 19Pushed 10y ago1 watchersCompare

[ Source](https://github.com/voltrue2/pie)[ Packagist](https://packagist.org/packages/pie/pie)[ RSS](/packages/pie-pie/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependenciesVersions (2)Used By (0)

pie Library
===========

[](#pie-library)

A very light weight PHP library to handle routing, controller management, SQL + memcache, and logging.

How To Use
----------

[](#how-to-use)

### Require pie Library

[](#require-pie-library)

```
require_once('pie/index.php');

```

### Create Bootstrap

[](#create-bootstrap)

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

Classes
-------

[](#classes)

These classes will be included when you you `pie` library.

### Loader

[](#loader)

A static class to handle `include`.

#### Static Methods

[](#static-methods)

##### ::setRootPath($path \[string\])

[](#setrootpathpath-string)

Set a root path for Loader to use as the root path.

##### ::get($path)

[](#getpath)

Static method to load a PHP source code.

Example:

```
Loader::get('my/awesome/php/lib.php');
```

##### ::getRootPath()

[](#getrootpath)

Static method to return the root path for Loader.

### Config

[](#config)

A static class to handle setting and getting configuration values across the application.

#### Static Methods

[](#static-methods-1)

##### ::set($name \[string\], $value \[mixed\])

[](#setname-string-value-mixed)

##### ::get($name \[string\])

[](#getname-string)

Returns the value of configuration by its name set by `.set()`.

### Router

[](#router)

A router class to handle routing.

#### Methods

[](#methods)

##### ::redirect($uri \[string\], $statusCode \[\*number\])

[](#redirecturi-string-statuscode-number)

A static method to redirect.

Example:

```
Router::redirect('/redirect/here/', 301);
```

##### -&gt;setTrailingSlash($enable \[boolean\])

[](#-settrailingslashenable-boolean)

Enable/Disable enforced trailing slash in URL.

##### -&gt;setControllerPath($path \[string\])

[](#-setcontrollerpathpath-string)

Set controller directory path where all controllers are.

##### -&gt;addReroute($from \[string\], $to \[$to\])

[](#-addreroutefrom-string-to-to)

Adds a rerouting path.

Exmaple:

```
$router->addReroute('/', '/reroute/here/');
```

The above example will reroute `/` to `/reroute/here/`.

##### -&gt;addErrorReroute($code \[number\], $controllerName)

[](#-adderrorreroutecode-number-controllername)

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.

##### -&gt;addRequestHook($uri \[string\], $funcName \[string\], $class \[\*mixed\])

[](#-addrequesthookuri-string-funcname-string-class-mixed)

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...
*/
```

### Console

[](#console)

A static class for logging both on server and client (browser).

#### Static Methods

[](#static-methods-2)

##### ::setup($filePath \[\*string\], $noClient \[\*boolean\], $verbose \[\*boolean\])

[](#setupfilepath-string-noclient-boolean-verbose-boolean)

Set up Console class.

###### $filePath

[](#filepath)

If given, Console will be logging to the path given on the server.

###### $noClient

[](#noclient)

If `false`, Console will not be logging in console of browser.

Default value is `false`.

###### $verbose

[](#verbose)

If `false` Console will not output `log`, but `warn` and `error` only.

Default value is `true`.

##### ::create($name \[\*string\])

[](#createname-string)

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

Controller
----------

[](#controller)

`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`.

### How To Create A Controller Class

[](#how-to-create-a-controller-class)

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

```

#### Controller Class

[](#controller-class)

A controller must be a valid PHP class such as:

```
