pie/pie

一个轻量级的路由器,支持memcache和SQL处理,以及会话管理库。

dev-master 2016-01-08 10:15 UTC

This package is not auto-updated.

Last update: 2024-10-02 10:12:13 UTC


README

一个用于处理路由、控制器管理、SQL + memcache和日志的非常轻量级的PHP库。

如何使用

需要pie库

require_once('pie/index.php');

创建引导

在你的index.php文件中,你需要包含以下内容:

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

当你包含pie库时,以下类将被包含。

加载器

一个静态类,用于处理include

静态方法

::setRootPath($path [string])

为加载器设置一个根路径作为根路径。

::get($path)

静态方法,用于加载PHP源代码。

示例

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

静态方法,用于返回加载器的根路径。

配置

一个静态类,用于处理整个应用程序中的配置设置和获取。

静态方法

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

通过.set()设置的名称返回配置值。

路由器

一个用于处理路由的路由器类。

方法

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

静态方法,用于重定向。

示例

Router::redirect('/redirect/here/', 301);
->setTrailingSlash($enable [boolean])

启用/禁用URL中的强制尾随斜杠。

->setControllerPath($path [string])

设置控制器目录路径,其中包含所有控制器。

->addReroute($from [string], $to [$to])

添加重定向路径。

示例

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

上面的示例将重定向//reroute/here/

->addErrorReroute($code [number], $controllerName)

将URI分配给特定的错误代码,例如404

$router->addErrorReroute(404, '/error/notfound/');

上面的示例将在每个404错误上执行/error/notfound/

->addRequestHook($uri [string], $funcName [string], $class [*mixed])

在指定的URI上注册callback函数。

注册的callback将在每个匹配的请求上被调用。

注意1: callback 必须返回HTTP状态码以处理错误。

如果没有错误在钩子中,你可以返回200或执行一些函数。

示例

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

}

注意2:添加到controller的钩子将仅对具有相同controller的所有请求执行。

示例

$router->addRequestHook('/example/', 'myHook');
// the above will be exected on:
/*
	/example/
	/example/index/
	/example/boo/
	/example/foo/ 
	etc...
*/

控制台

一个静态类,用于在服务器和客户端(浏览器)上记录。

静态方法

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

设置控制台类。

$filePath

如果提供,控制台将在服务器上记录到指定的路径。

$noClient

如果为false,则控制台不会在浏览器控制台中记录。

默认值为false

$verbose

如果为false,则控制台不会输出log,但仅输出warnerror

默认值为true

::create($name [*string])

返回用于记录的ConsoleCore对象的实例。

示例

Console::init('/logging/path/', true);
$console = Console::create();

$console->log('boo');
$console->warn('foo');
$console->error('too');

注意:控制台将捕获未捕获的异常并自动记录错误。

控制器

pie库通过相应的controller处理每个请求。

例如,URL /example/index将在controller/example/index.class.php中的控制器类中执行。

如何创建控制器类

首先,你必须设置控制器路径,如下所示:

$router = new Router();
$router->setControllerPath('path/to/my/controller/');

然后,通过在控制器目录中创建一个控制器目录和一个方法文件来定义URI。

# Define controller 'example'
mkdir path/to/my/controller/example
# A controller method called index
path/to/my/controller/example/index.class.php

控制器类

控制器必须是一个有效的PHP类,如下所示:

<?php

class Controller {

	public function __construct() {
		
	}

	public function GET($request, $response, $params) {

	}

}

公共方法GET将处理GET /example/index

类名必须Controller

每个控制器方法将有三个参数

请求

请求 是 Request 类的一个实例。

->getData($dataName [字符串])

返回 GET/POST/PUT/DELETE 的匹配请求数据。

->getAllData()

返回 GET/POST/PUT/DELETE 的所有请求数据映射。

->getHeader($name [字符串])

返回匹配的请求头。

->getAllHeaders()

返回所有请求头。

响应
->assign($name [字符串], $value [混合])

为响应输出分配变量。

->html($source [字符串], $statusCode [*数字])

如果 $source 是模板文件的路径,它将加载它并输出其内容。

如果 $source 是字符串值,它将原样输出。

->json($statusCode [*数字])

将分配的变量(通过 .assign())作为 JSON 字符串输出。

->redirect($uri [字符串], $statusCode [*数字])

重定向到给定的 $uri,带有给定的 状态码

如果未提供 状态码,则默认为 '301'。

参数

URL 参数数组。

示例

/example/index/one/two/three

$params = array(
	'one',
	'two',
	'three'
);

数据

一个静态类,用于在控制器到模板上输出“已分配”的值。

静态方法

::get($assignedName [字符串])

返回通过 $response->assign() 分配的值。

示例

// in your controller
$response->assign('boo', 'Boo');
<!-- in your HTML template -->
<?= Data::get('boo') ?>

数据源

pie 库附带一个非常简单的 SQL + Memcache 类。

如何设置数据源

我们需要正确设置 数据源

注意: 数据源 支持 mysqlpgsql

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

处理 SQL

使用 数据源,我们有数据 模型

$myModel = DataSource::get('myModel');

数据源类

静态方法

::create(dataModelName [字符串])

创建一个数据模型的新实例以访问 SQL。

DataSource::create('myDataModel');
::get(dataModelName [字符串])

返回由 ::create() 创建的数据模型实例。

DataSource::create('myDataModel');
$model = DataSource::get('myDataModel');

模型类

该类用于处理 SQL 查询和 memchache。

方法

->setupCache($host [字符串], $port [数字], $ttl [数字])

设置 memcache 连接和 TTL(以秒为单位)。

->ignoreCache()

禁用 memcache。

->setupMaster($type [字符串], $host [字符串], $dbName [字符串], $user [字符串], $password [字符串])

设置 SQL 主连接。

$type = 'mysql' 或 'pqsql'
->setupSlave($type [字符串], $host [字符串], $dbName [字符串], $user [字符串], $password [字符串])

设置 SQL 从连接。

$type = 'mysql' 或 'pqsql'
->read($sql [字符串], $params [*数组])

返回查询结果。

此方法从 读取。

使用 Memcache。

->readForWrite($sql [字符串], $params [*数组]);

返回查询结果。

此方法从 读取。

Memcache 使用。

->write($sql [字符串], $params [*数组])

上执行查询,如果处于 事务 中,则异常错误将自动回滚。

更新 memcache 时间。

->transaction()

开始事务。

->commit()

提交事务查询。

->rollback()

回滚事务查询。

Uid 类

一个用于创建唯一 ID 的静态类。

ID 是一个字符串。

$uid = Uid::create();

静态方法

::create()

返回一个唯一的 ID 字符串。

Encrypt 类

一个用于创建散列并验证的静态类。

用于安全密码验证。

$pass = 'secret';
$hash = Encrypt::createHash($pass);
$validated = Encrypt::validateHash($pass, $hash);

静态方法

::createHash($password [字符串])

返回给定参数 $password 的散列。

::validateHash($password [字符串], $hash [字符串])

验证散列和给定参数 $password 并返回一个布尔值。

Session 类

一个用于处理会话的静态类。

静态方法

::setup($domain [字符串], $prefix [字符串], $host [字符串], $port [数字], $ttl [数字])

$ttl 以秒为单位。

::get()
::set($value [混合])
::delete()

ExceptionHandler 类

一个用于注册和处理未捕获异常的静态类。

静态方法

::add($func [字符串], $class [*混合])