phpf / micro
使用Phpf组件的PHP微框架。
Requires
- php: >=5.3
- ext-pdo: *
- phpf/httputil: dev-master
- wells5609/php-util: dev-master
This package is not auto-updated.
Last update: 2024-09-14 15:00:52 UTC
README
使用Phpf组件的PHP微框架。
####目标
- 易于理解和使用的疯狂简单
- 卓越的基准性能
- 最小化依赖
####要求
- PHP 5.3+
- PDO扩展
- Phpf/HttpUtil
- wells5609/PHP-Util
###概述
"Micro"(工作名称)是一个为小型但可能复杂的Web应用程序而设计的最小框架。PHP并不真正需要另一个框架,"micro"或其他,但我发现目前可用的选项过于有限或具有指导性。
####理念 主要观点
- 像PHP一样编写PHP - 框架不应感觉像另一种语言
- 简短 - 更大的代码库并不能帮助你写出更好的代码
- 简单 - 开发者应该把时间花在开发上,而不是学习框架
###入门
像大多数框架一样,你需要为你的服务器设置适当的重写规则,以便所有请求都路由到一个文件(例如index.php
)。
####1. 配置
使用.htaccess
或web.config
或其他,你需要一个应用引导。引导从用户配置文件开始,该文件必须返回一个关联数组。
将文件路径传递给Phpf\App::configure()
方法,它将返回一个带有默认值的数组,为缺失的键填充。对配置数组做一些事情(例如创建一些常量等)或者继续。
// configuration settings $conf = \Phpf\App::configure(__DIR__.'/user_config.php');
####2. 创建应用
接下来,通过将数组传递给Phpf\App::createFromArray()
方法来生成应用
$app = \Phpf\App::createFromArray($conf);
返回的对象,$app
,是Phpf\App
的一个实例。在这个时候,对象设置了以下属性,这些属性是从配置数组中收集的
id
(字符串)- 应用实例的ID(Phpf\App
是一个多例)。namespace
(字符串)- 应用资源的作用域(例如模型、控制器等)。你创建所有这些类。components['env']
(Phpf\Common\Env
)- 包含环境信息的对象,包括字符集、时区和调试设置。这些信息用于设置错误报告、默认日期时区和mbstring扩展编码(如果启用)。它还从配置数组中在键dirs
下列出的任何目录创建完整文件路径(默认情况下,使用目录名创建常量)。components['aliaser']
(Phpf\Common\ClassAliaser
)- 用于延迟创建类别名。别名是从配置数组(键aliases
)中的嵌套数组创建的,别名/真实类(完全解析)名称。components['config']
(默认:Phpf\Config
)- 包含应用配置设置的对象。默认情况下,该对象是Phpf\Config
的一个实例,但可以通过设置类别名Config
来更改。- 为应用作用域创建了一个PSR-0自动加载器,其基本路径设置为
dirs['app']
配置设置给出的路径之上的一级。
####3. 设置组件
####缓存!以上内容对所有组件都适用,除了缓存。缓存需要一个驱动程序(例如XCache的驱动程序是预包装的),用户可以通过设置配置项 cache['driver-class']
来提供(例如:$config['cache']['driver-class'] = 'My_SomeCacheEngine_Driver'
)。当使用 $app->startCache()
启动缓存时,会实例化驱动程序。如果您想使用自己的(单例)缓存而不是 Phpf\Cache
,请将字符串名称(或直接使用别名 Cache
)传递给 startCache()
方法。
使用别名来实例化组件对象(有关默认组件别名的列表,请参阅 Phpf\App::configure()
)。这允许在不更改大量文件的情况下轻松交换组件类。
组件加载过程可能如下所示
$app->startCache('Cache'); // string => singleton ('Cache' is an alias for 'Phpf\Cache'). $app->set('session', $session = new \Session); $session->setDriver(new \Phpf\Session\Driver\Native)->start(); // a handler for native PHP sessions is pre-packaged $app->set('events', $events = new \Events); $app->set('router', new \Router($events)); // Router uses Events to call pre- and post-dispatch actions $app->set('filesystem', $filesystem = new \Filesystem(APP)); // APP was set by Env via 'app' dir in config $app->set('packageManager', $pkgMgr = new \Phpf\PackageManager($app)); $pkgMgr->init(); // loads packages specified in user config $app->set('request', $request = \Request::createFromGlobals()); $request->setSession($session); $app->set('response', $response = new \Response); $response->setRequest($request); // Response uses request to negotiate content type $app->set('viewManager', $viewMgr = new \Phpf\ViewManager($filesystem)); // ViewManager uses filesystem to locate views $viewMgr->setEvents($events); // ViewManager uses events to call actions before rending views. // [Optional] add filesystem directories // 'VIEWS' is set in Env // 'views' is group name, referenced when finding files // '2' is recursion depth to use when searching for files in the directory. $filesystem->add(VIEWS, 'views', 2);
####5. 设置现在您需要包含一些文件来操作组件(例如,添加路由、设置数据库表模式等)。目前,请参考每个组件的文档。
####6. 路由包含这些文件后,派遣请求
$app->router->dispatch($app['request'], $app['response']);
您可能会注意到,可以通过对象(->
)或数组([]
)语法访问应用程序组件。您还可以使用 get()
方法,它将返回指定名称的组件,如果存在的话。
当路由器找到一个匹配的路由时,它会实例化路由控制器,并将请求(Request)和响应(Response)对象(以及通过 dispatch
事件添加的任何其他对象)作为属性(分别为'request'和'response')提供给控制器。然后,它调用路由控制器的回调方法,在该方法中填充响应体。
响应体填充后,我们只需将其发送给用户
$app->response->send();
就是这样。