sugiphp/sugi
SugiPHP 微型框架
v0.0.1
2014-09-12 13:12 UTC
Requires
- php: >=5.3
- leafo/lessphp: 0.3.*
- sugiphp/assets: dev-master
- sugiphp/cache: dev-master
- sugiphp/config: dev-master
- sugiphp/container: dev-master
- sugiphp/cron: dev-master
- sugiphp/database: dev-master
- sugiphp/events: dev-master
- sugiphp/filesystem: dev-master
- sugiphp/filter: dev-master
- sugiphp/http: dev-master
- sugiphp/logger: dev-master
- sugiphp/routing: dev-master
- swiftmailer/swiftmailer: 4.3.*
- symfony/yaml: 2.2.*
This package is not auto-updated.
Last update: 2024-09-23 06:47:20 UTC
README
Sugi 是一个使用 SugiPHP 组件以及一些其他组件的微型框架。它作为依赖注入容器工作,非常灵活。
Hello World! 示例
<?php $app = new \SugiPHP\Sugi\App(); $app->route("HelloWorld", "/hello", function () { echo "Hello World!"; }); $app->run(); ?>
安装
composer require sugiphp/sugi:dev-master
用法
定义的路径
<?php use SugiPHP\Sugi\App; // Instantiate SugiPHP Application: $app = new App(["base_path" => dirname(__DIR__) . "/"]); // Or use Singleton pattern: $app = App::getInstance(); ?>
PSR-3 日志记录器
应用程序默认使用SugiPHP\Logger,它符合PSR-3规范。要使用日志记录器,可以使用一个懒加载方法:log(string $level, string $message, array $context = [])
,或者使用实例app["logger"]
来访问规范中给出的方法
<?php $app->log("debug", "Debug message"); $app->log("info", "user input was", $_POST); $app["logger"]->error("my error message"); $app["logger"]->setLevel("warning"); $app["logger"]->info("this will not be logged"); ?>
PSR-7 请求
应用程序与 PSR-7 兼容,并使用内部zendframework/zend-diactoros。您可以为 ServerRequest(\Psr\Http\Message\ServerRequestInterface 实例)连接任何其他 PSR-7 兼容库
<?php $app["request"] = new \Your\ServerRequest(); ?>
URI 是 \Psr\Http\Message\UriInterface 实例,因此您可以使用
<?php $app["uri"]->getHost(); $app["uri"]->getPath(); ?>
以及其他所有 PSR-7 UriInterface 方法。请注意,操作 $app["uri"] 不会改变其值
<?php echo $app["uri"]->getPath(); // "/" echo $app["uri"]->withPath("/foo/bar")->getPath(); // "/foo/bar" echo $app["uri"]->getPath(); // "/" // to override it: $app["uri"] = $app["uri"]->withPath("/foo"); echo $app["uri"]->getPath(); // "/foo" ?>
缓存
默认缓存是虚拟缓存,仅在脚本结束时可用。所有值都可以使用一个方法 cache()
设置和获取
<?php $app->cache("key", "value"); echo $app->cache("key"); // returns "value" // to access all methods for the cache you can use: $app["cache"]->has("key"); // returns TRUE ?>
默认情况下,Sugi 使用 SugiPHP Cache。要配置 Sugi 以使用 memcached 服务器,您可以使用配置文件 /path/to/config/cache.php
return [
"store" => "memcached",
"host" => "127.0.0.1",
"port" => 11211,
"prefix" => "myprefix",
]
查看配置文件示例