使用 hyperf 已經有一段時間了,下面是一些常用的助手函數,分享一下~~~
<?php use HyperfContractStdoutLoggerInterface; use HyperfHttpServerContractResponseInterface; use HyperfLoggerLoggerFactory; use HyperfServerServerFactory; use HyperfUtilsApplicationContext; use PsrHttpMessageServerRequestInterface; use SwooleWebsocketFrame; use SwooleWebSocketServer as WebSocketServer; /** * 容器範例 */ if (!function_exists('container')) { function container() { return ApplicationContext::getContainer(); } } /** * redis 用戶端範例 */ if (!function_exists('redis')) { function redis() { return container()->get(Redis::class); } } /** * server 範例 基於 swoole server */ if (!function_exists('server')) { function server() { return container()->get(ServerFactory::class)->getServer()->getServer(); } } /** * websocket frame 範例 */ if (!function_exists('frame')) { function frame() { return container()->get(Frame::class); } } /** * websocket 範例 */ if (!function_exists('websocket')) { function websocket() { return container()->get(WebSocketServer::class); } } /** * 快取範例 簡單的快取 */ if (!function_exists('cache')) { function cache() { return container()->get(PsrSimpleCacheCacheInterface::class); } } /** * 控制台紀錄檔 */ if (!function_exists('stdLog')) { function stdLog() { return container()->get(StdoutLoggerInterface::class); } } /** * 檔案紀錄檔 */ if (!function_exists('logger')) { function logger() { return container()->get(LoggerFactory::class)->make(); } } /** * */ if (!function_exists('request')) { function request() { return container()->get(ServerRequestInterface::class); } } /** * */ if (!function_exists('response')) { function response() { return container()->get(ResponseInterface::class); } }
推薦教學:《PHP教學》
以上就是分享幾個 Hyperf 常用助手函數的詳細內容,更多請關注TW511.COM其它相關文章!