ennerd / raw
dev-master
2023-05-22 08:40 UTC
Requires
- guzzlehttp/guzzle: ^7.6
- psr/log: ^3.0
This package is auto-updated.
Last update: 2024-09-22 11:28:17 UTC
README
一个非常小的框架,用于高效编写PHP Web应用程序。该框架侧重于生产力,并优先考虑这一点,而大多数现代框架往往优先考虑过度工程。
函数参考
/**
* Shared Cache, where cached values are available across requests and multiple
* servers.
*/
Raw::cache(); // Returns an instance of Raw\Cache
Raw::cache()->set($key, $value, $ttl); // Cache some value in a shared cache
Raw::cache()->get($key, &$found=null); // Get a cached value from the shared cache
Raw::cache()->has($key); // Check if a shared cached value exists
Raw::cache()->unset($key); // Remove a value from the shared cache
/**
* Shared Configuration, where configuration values are shared across all requests
* on the same server.
*/
Raw::config(); // Returns an instance of Raw\Config
Raw::config()->rootPath; // The path to the folder where composer.json is stored
Raw::config()->databaseUser; // The value of MYSQL_USER environment variable
Raw::config()->databasePassword; // The value of MYSQL_PASSWORD environment variable
Raw::config()->databaseHost; // The value of MYSQL_HOST environment variable
Raw::config()->databaseName; // The value of MYSQL_DATABASE environment variable
Raw::config()->databaseDSN; // The database DSN (a PDO DSN string constructed from environment variables)
/**
* Context, where all information pertaining to the specific request being processed
* is stored. If your application is a ReactPHP or Swoole application, it is essential
* that you access the context via this method, instead of storing data in a global
* variable.
*/
Raw::context(); // Returns an instance of Raw\Context
Raw::context()->db(); // Connects to the database and returns an instance of Raw\DB
Raw::context()->db()->query($sql, ...$vars); // Performs an SQL query and returns a Traversable<stdClass>
Raw::context()->db()->queryField($sql, ...$vars); // Performs an SQL query and returns a single value
Raw::context()->db()->queryRow($sql, ...$vars); // Performs an SQL query and returns an stdClass instance
Raw::context()->db()->exec($sql, ...$vars); // Performs an update query and returns the number of affected rows
Raw::context()->db()->lastInsertId(); // Returns the last inserted auto increment id
Raw::context()->pdo(); // Connects to the database and returns an instance of \PDO
Raw::context()->cache(); // Returns an instance of Raw\Cache
Raw::context()->cache()->set($key, $value, $ttl); // Cache some value in a short lived per-request cache
Raw::context()->cache()->get($key, &$found=null); // Get a cached value from the short lived per-request cache
Raw::context()->cache()->has($key); // Check if a key exists in the short lived per-request cache
Raw::context()->cache()->unset($key); // Remove a value from the short lived per-request cache
Raw::context()->request(); // Return the instance of Raw\Request which is currently being processed
Raw::context()->request()->get($key); // Return the value of the query parameter $key
Raw::context()->request()->post($key); // Return the value of the http post data form field $key
Raw::context()->request()->header($key); // Return the value of the http request header $key
Raw::context()->request()->method(); // Return the request verb ("GET", "POST", "PUT" etc)
Raw::context()->response(); // Return the instance of Raw\Response which handles sending a response
Raw::context()->response()->header($key, $value, $replace=false); // Set an HTTP response header
Raw::context()->response()->status($code, $reason=null); // Set the response status code (200, 404 etc)
Raw::context()->response()->cacheTTL($ttl) // Reduce the cacheability of this response to $ttl seconds
Raw::context()->response()->cachePublic($ttl=null); // set Cache-Control: public, max-age=$ttl unless previously private/no-cache
Raw::context()->response()->cachePrivate($ttl=null); // Set Cache-Control: private, max-age=$ttl unless previously no-cache
Raw::context()->response()->cacheOff($noStore=false); // Set Cache-Control: no-cache or no-store
Raw::context()->response()->write($chunk); // Write a string to the response body
Raw::context()->response()->end($chunk=null); // Close the response (optionally writing a string to the response body)
/**
* Logger is used for logging events and activities.
*/
Raw::logger()->emergency($message, $context=[]);
Raw::logger()->alert($message, $context=[]);
Raw::logger()->critical($message, $context=[]);
Raw::logger()->error($message, $context=[]);
Raw::logger()->warning($message, $context=[]);
Raw::logger()->notice($message, $context=[]);
Raw::logger()->info($message, $context=[]);
Raw::logger()->debug($message, $context=[]);
Raw::logger()->log($level, $message, $context=[]);
/**
* Session is an object which is saved across multiple requests per browser.
*/
Raw::session(); // Creates a new session if one does not already exist and return a Raw\Cache instance
Raw::session()->set($key, $value, $ttl); // Store a value in the session
Raw::session()->get($key, &$found=null); // Get a value from the session
Raw::session()->has($key); // Check if a key exists in the session
Raw::session()->unset($key); // Remove a value from the session
路由和请求处理
Raw框架对性能和可扩展性有很强的关注。因此,除非需要,框架将不会加载或实例化控制器。
如果收到对“基本路由”的请求,将实例化控制器。基本路由是控制器将处理请求的最短路径。
示例基本路由
/users
将不带任何参数实例化控制器。/users/<id>
将使用从路径传递给构造函数的id实例化控制器。
多个控制器可以订阅相同的路由;每个控制器都有一个内部路由器,将确定调用该控制器的方法。
示例控制器
use Raw\Route;
class Users extends Raw\Controller {
#[Route("/")]
public function index() {
return "Lists all users";
}
#[Route("/:id")]
public function retrieve(int $id) {
return "User id $id";
}
/**
* A handler can be annotated with multiple routes
*/
#[Route("/:id", ["POST"])]
#[Route("/:id/edit", ["GET", "POST"])]
public function edit(int $id) {
return "Edit user id $id";
}
#[Route("/:id", ["DELETE"])]
#[Route("/:id/delete", ["GET", "POST"])]
public function delete(int $id) {
return "Delete user id $id";
}
}
当类已经声明后,您可以挂载控制器
Raw::router()->mount('/users', Users::class);
当请求URL与 /users
匹配时,将加载控制器。对 /users/123
的请求将调用 User::retrieve()
方法。
配置参考
Raw框架通过环境变量进行配置。以下是框架尊重的环境变量的完整参考。所有配置选项都可通过 Raw::config()->*
变量访问。
- MYSQL_HOST:MySQL服务器的计算机名或IP地址。
- MYSQL_PORT:MySQL服务器监听的端口号。默认为3306。
- MYSQL_DATABASE:您希望应用程序连接到的MySQL数据库的名称。
- MYSQL_USER:MySQL数据库的用户名。
- MYSQL_PASSWORD:MySQL数据库的密码。
- MYSQL_CHARSET:用于MySQL数据库连接的字符集。如果未指定,默认字符集为"utf8mb4"。
- HTTP_PROXY:指定应用程序发出的HTTP请求要使用的HTTP代理。如果未指定,则不使用代理。
- HTTP_CLIENT_TIMEOUT:HTTP客户端请求的超时时间(以秒为单位)。如果未指定,默认超时设置为10秒。
- HTTP_CLIENT_USER_AGENT:用于HTTP客户端请求的用户代理字符串。如果未指定,则默认用户代理字符串为'Raw/1.0'。