ECO是PHP 5.5+的PHP框架
安装示例:
# wget shayanderson.com/eco.zip
# unzip ./eco.zip
可以在app/com/route.php中设置基本路由,例如:
// set routes
return [
// class method
' / ' => ' IndexController->home ' ,
// namespace
' login ' => ' AccountUserController->login ' ,
// callable
' logout ' => function () { /* do something */ },
// more routes
];请求/将在File app/mod/IndexController.php中的类IndexController中调用方法home 。
请求/login将在File app/mod/Account/UserController.php中的类AccountUserController中调用方法login 。
特定的HTTP方法可用于路由:
eco:: route ( ' GET@api/user/:id ' , function ( $ id ) { /* GET method */ });
eco:: route ( ' DELETE@api/user/:id ' , function ( $ id ) { /* DELETE method */ });
eco:: route ( ' POST@api/user/:id? ' , function ( $ id = null ) { /* POST method */ });
// or handle multiple HTTP methods
eco:: route ( ' POST|PUT@api/user/:id? ' , function ( $ id = null ) { /* POST or PUT method */ });支持这些HTTP方法:
DELETE,GET,HEAD,PATCH,POST,PUT
路由回调可以是可呼叫的名称或可呼叫(第一个数组元素必须是控制器/操作):
function auth () { /* do something */ }
eco:: route ( ' account/secure ' ,
[ ' AccountController->secure ' , ' auth ' , function () { /* invoked */ }]);警告:路由回调不适用于动态路线设置
动态路由设置器可用于具有许多路由(懒负载路由)的较大应用程序,例如:
eco:: route ( ' account* ' , ' AccountController::getRoutes() ' );现在,以/account开头的所有请求将使用该方法加载路线:
class AccountController
{
public static function getRoutes ()
{
return [
' account/login ' => ' AccountController->login ' ,
// do not have to use class name if in same class:
' account/logout ' => ' logout '
];
}
}动态路由设置(懒负载路由)也可以直接设置为数组,例如:
eco:: route ( ' account* ' , [
' AccountController ' , // the class name must be first (index:0)
' account/login ' => ' login ' ,
' account/logout ' => ' logout '
]);CLI路由很容易使用,例如:
eco: route ( ' bin/test/:id ' , function ( $ id ) {
echo ' CLI test, id: ' . $ id ;
});现在可以发出CLI命令:
$ php index.php bin/test/5
Bin test: 5
上述路线也将在Web浏览器中工作。为了创建仅CLI路由(将在Web浏览器中不起作用)为路由的开头添加
$:例如:
// this route will only work as CLI command
// the request '/bin/test/5' in a Web browser would result in a 404 error
eco: route ( ' $bin/test/:id ' , function ( $ id ) {
echo ' CLI test, id: ' . $ id ;
});命名路由参数:
eco:: route ( ' user/:id ' , ' UserController->view ' ); :id值将传递给可呼叫或类方法:
class UserController
{
public function view ( $ id )
{
// do something
}
}可选参数可以使用:
eco:: route ( ' page/:id/:title? ' , function ( $ id , $ title = null ) {});通配符参数示例:
eco:: route ( ' product/:info+ ' , function ( $ info ) {
// if request is '/product/1/abc/x'
// $info is an array: ['1', 'abc', 'x']
});路由参数回调可以以几种不同的方式使用:
eco:: route ( ' page/:title:strtoupper ' , ' PageController->view ' );或在数组中设置参数回调(第一个数组元素必须是类方法 /可呼叫):
eco:: route ( ' page/:title ' , [ ' PageController->view ' , [ ' title ' => ' strtoupper ' ]]);另外,可以设置全局路由参数回调,例如:
// globally set route param callback
// now every ':id' param will use this callback
// global param callbacks are overwritten by local ones
eco:: param ( ' id ' , function ( $ id ) { return strtoupper ( $ id ); });
// or use multiple param names:
eco:: param ([ ' x ' , ' y ' , ' z ' ], function ( $ val ) { });路由回调也可以与路由参数回调一起使用:
eco:: route ( ' page/:title ' , [
' PageController->view ' ,
' route_callback ' ,
function () { /* route callback */ },
// route param callback
[ ' title ' => ' strtoupper ' ]
]);可以使用路由参数正则表达式,如果匹配失败,则不会调用路由:
eco:: route ( ' user/:id@[0-9]+ ' , ...);使用不同类型的参数和/或组合回调和正则表达式时,语法顺序很重要,这是正确的语法:
// when using callback with regular expression
// the callback must come first:
eco:: route ( ' user/:id:strtoupper@[0-9]+ ' , ...);
// use optional param and callback:
eco:: route ( ' user/:id/:name?:strtoupper ' , ...);
// use optional param and regular expression:
eco:: route ( ' user/:id/:name?@[a-z]+ ' , ...);
// use optional param, callback and regular expression:
eco:: route ( ' user/:id/:name?:strtoupper@[a-z]+ ' , ...);
// use wildcard param and callback:
eco:: route ( ' user/:params+:strtoupper ' , ...);
// use wildcard param and regular expression:
eco:: route ( ' user/:params+@[a-z]+ ' , ...);
// use wildcard param, callback and regular expression:
eco:: route ( ' user/:params+:strtoupper@[a-z]+ ' , ...);视图对象可以在路由操作中使用:
class PageController
{
public function get ( $ id )
{
$ page = PageModel:: get ( $ id );
// set view param
eco:: view ()-> set ( ' title ' , $ page -> title );
// or use view() helper function
// view()->set('title', $page->title);
// or like this:
view ()-> author = $ page -> author ;
// load template file 'page/view'
// and can also set view param 'content'
view ( ' page/view ' , [
' content ' => $ page -> content
]);
}
}路由参数也可以使用
Router类方法访问。
app/tpl/page/view.tpl文件示例:
<?php include PATH_TEMPLATE_GLOBAL . ' header.tpl ' ; ?>
<h1> <?= $ title ?> </h1>
<p>Author: <?= $ author ?> </p>
<?= $ content ?>
<?php include PATH_TEMPLATE_GLOBAL . ' footer.tpl ' ; ?> 记录消息示例:
eco:: log ()-> error ( ' Bad error / fatal ' );
eco:: log ()-> warning ( ' Warning conditions ' );
eco:: log ()-> notice ( ' Notice message ' );
eco:: log ()-> debug ( ' File has loaded ' );记录消息时可以使用类别:
// category 'payment'
eco:: log ()-> error ( ' Failed to access payment gateway ' , ' payment ' );还可以添加调试信息:
eco:: log ()-> warning ( ' Request failed ' , /* category optional */ null ,
[ ' request ' => ' x ' , ' client ' => ' y ' ]);获取整个日志:
$ log = eco:: log ()-> get ();设置自定义日志处理程序示例:
eco:: log ()-> setHandler ( function ( $ message , $ level , $ category , $ info ){
// if sending message to database
// do no send if database connection error, example:
// if(substr($message, 0, 17) == 'SQLSTATE[HY000] [')
// {
// return false;
// }
// handle custom logging
return true ; // handled
});如果自定义日志处理程序无法返回
true则内部记录器将按照正常继续继续
触发错误示例:
eco:: error ( ' Error message ' );
// or use helper function
error ( ' Something bad ' );
// custom error code
error ( ' Page not found ' , 404 );
// add log category
error ( ' Failed to load user ' , null , ' account ' );
// by default the HTTP error response code is sent
// to not send HTTP code use:
error ( ' Page not found ' , 404 , ' category ' false ); eco::error()使用:
$ error_message = eco:: errorGetLast ();
$ error_category = eco:: errorGetLastCategory ();在调用错误之前,可以使用404回调来处理404
钩子可用于加载文件或在执行过程中使用回调,例如:
// called before routing starts
eco:: hook (eco:: HOOK_BEFORE , function () { /* do something */ });
// called before the route callable or class method is called
// include a file example:
eco:: hook (eco:: HOOK_MIDDLE , PATH_LIB . ' hook/middle.php ' );
// called after dispatching
eco:: hook (eco:: HOOK_AFTER , function () { /* do something */ });应用程序和ECO配置设置分别处理。
ECO配置设置文件位于app/com/conf/eco.conf.php上,并包含所有框架设置。所有有关ECO配置设置的文档都可以在文件中找到。
应用程序配置设置可以通过ECO存储,例如:
// load config file(s) using path
eco:: conf ( PATH_CONF . ' app.conf.php ' );
eco:: conf ( PATH_CONF . ' api.conf.php ' );
// or load config settings using array
eco:: conf ([ ' local ' => [ ' path ' => ' x ' , ' filename ' => ' y ' ]]);
// use config settings
$ app_username = eco:: conf ()-> app -> username ;
$ local_path = eco:: conf ()-> local -> path ;配置文件必须返回数组, app.conf.php示例:
return [
' app ' => [
' username ' => ' x ' ,
' password ' => ' y '
],
' core ' => [ /* more */ ]
];使用多个文件时,配置设置不能被覆盖,因此即使在不同的文件中,主数组键也必须是唯一的。切勿使用ECO用于内部框架设置的主数组密钥
_eco。
配置设置也可以与ECO分开使用,例如:
// do not store internally
$ conf = eco:: conf ( PATH_CONF . ' app.conf.php ' , false );
// use config settings
$ app_username = $ conf -> app -> username ;ECO提供以下方法:
eco::autoload($paths) - 类自动加载器eco::breadcrumb() - 访问面包屑类( breadcrumb()可用的辅助功能)eco::clear($key) - 清除全局变量eco::conf($file_path, $store) - 注册 /加载应用程序配置设置文件( conf()助手功能可用)eco::db($connection_id) - 访问数据库类( db()辅助功能可用)eco::error($message, $code, $category, $http_response_code) - 触发错误( error()助手功能可用)eco::filter() - 访问数据过滤器类( filter()辅助功能可用)eco::flash() - 访问会话flash类( flash()辅助功能可用)eco::format() - 访问数据格式类( format()辅助功能可用)eco::get($key) - 获取全局变量eco::has($key) - 检查是否存在全局变量eco::hook($name, $callback) - 添加钩子eco::log() - 访问日志类( logger()可用辅助功能)eco::model() - 模型类加载程序( model()辅助功能可用)eco::param($id, $callback) - 地图路由参数回调( param()可用辅助功能)eco::request() - 访问请求类( request()可用辅助功能)eco::response() - 访问响应类( response()辅助功能可用)eco::route($route, $action) - 地图路线eco::router() - 访问核心路由器类eco::run() - 运行应用程序eco::service() - 服务类Loader( service()可用辅助功能)eco::session() - 访问会话类( session()助手功能可用)eco::set($key, $value) - 设置全局变量eco::stop() - 优雅地停止应用程序( stop()辅助功能)eco::validate() - 访问数据验证类( validate()辅助功能可用)eco::view($template, $view_params) - 加载视图模板文件和访问视图类( view()辅助功能可用) 核心类可用于简化常见的应用程序任务:
EcoCache服务器端缓存核心类EcoDatabase - 数据库核心类EcoForm -html表格核心类EcoHttp -HTTP请求核心类EcoModel - 数据库模型核心类EcoTable -HTML表核心类这些辅助课程可用:
EcoBenchmark基准辅助器EcoSystemBreadcrumb与eco::breadcrumb()或breadcrumb()辅助功能访问EcoSystemFilter eco::filter()或filter()辅助功能访问EcoSystemSessionFlash使用eco::flash()或flash()辅助功能访问EcoSystemFormat - 使用eco::format()或format()辅助功能访问EcoSystemRequest eco::request()或request()辅助功能访问EcoSystemRegistryService使用eco::service()或service()助手函数访问EcoSystemSession通过eco::session()或session()助手函数访问EcoSocketClient和EcoSocketServer创建套接字帮助者EcoSystemValidate -acccess with eco::validate()或validate()辅助功能助手功能可用于快速访问Eco Core方法或其他有用功能。在此处找到助手功能文档。
可以通过扩展核心EcoSystem类来扩展ECO。最初,这是app/com/bootstrap.php文件中的设置:
// set Eco access class
class eco extends Eco System {}扩展类可用于添加方法或覆盖EcoSystem超固化方法。
^回到顶部