hyperf的使用
hyperf是swoole的封装框架,用起来效率还是不错的.
使用方式看手册
https://hyperf.wiki/2.2/#/zh-cn/quick-start/install
其实是靠composer
composer create-project hyperf/hyperf-skeleton
之后就是会询问安装那些你想要的包
然后我就同意了 redis cache tarce等等,之后就是要加载模板啥的
用这个 composer require hyperf/view php bin/hyperf.php vendor:publish hyperf/view
还有 composer require hyperf/task
安装blade引擎
composer require hyperf/view-engine
安装thinkTemplate引擎
composer require sy-records/think-template
这个引擎需要写个文件来实现他接口(vendor/sy-records/think-template/src/TemplateEngine.php)

<?php declare(strict_types=1);
namespace think; use Hyperf\View\Engine\EngineInterface;
use think\Template; class TemplateEngine implements EngineInterface
{
public function render($template, $data, $config): string
{
// 实例化对应的模板引擎的实例
$engine = new Template($config);
// 并调用对应的渲染方法
return $engine->fetch($template, $data);
}
}
之后配置composer.json如下

{
"name": "hyperf/hyperf-skeleton",
"type": "project",
"keywords": [
"php",
"swoole",
"framework",
"hyperf",
"microservice",
"middleware"
],
"description": "A coroutine framework that focuses on hyperspeed and flexible, specifically use for build microservices and middlewares.",
"license": "Apache-2.0",
"require": {
"php": ">=7.3",
"hyperf/cache": "~2.2.0",
"hyperf/command": "~2.2.0",
"hyperf/config": "~2.2.0",
"hyperf/database": "~2.2.0",
"hyperf/db-connection": "~2.2.0",
"hyperf/framework": "~2.2.0",
"hyperf/guzzle": "~2.2.0",
"hyperf/http-server": "~2.2.0",
"hyperf/logger": "~2.2.0",
"hyperf/memory": "~2.2.0",
"hyperf/model-cache": "~2.2.0",
"hyperf/process": "~2.2.0",
"hyperf/redis": "~2.2.0",
"hyperf/task": "^2.2",
"hyperf/tracer": "~2.2.0",
"hyperf/view": "^2.2",
"hyperf/view-engine": "^2.2",
"sy-records/think-template": "^2.0"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^3.0",
"hyperf/devtool": "~2.2.0",
"hyperf/ide-helper": "~2.2.0",
"hyperf/testing": "~2.2.0",
"mockery/mockery": "^1.0",
"phpstan/phpstan": "^0.12",
"swoole/ide-helper": "^4.5"
},
"suggest": {
"ext-openssl": "Required to use HTTPS.",
"ext-json": "Required to use JSON.",
"ext-pdo": "Required to use MySQL Client.",
"ext-pdo_mysql": "Required to use MySQL Client.",
"ext-redis": "Required to use Redis Client."
},
"autoload": {
"psr-4": {
"App\\": "app/"
},
"files": []
},
"autoload-dev": {
"psr-4": {
"HyperfTest\\": "./test/"
}
},
"minimum-stability": "dev",
"prefer-stable": true,
"config": {
"optimize-autoloader": true,
"sort-packages": true
},
"extra": [],
"scripts": {
"post-root-package-install": [
"@php -r \"file_exists('.env') || copy('.env.example', '.env');\""
],
"post-autoload-dump": [
"rm -rf runtime/container"
],
"test": "co-phpunit --prepend test/bootstrap.php -c phpunit.xml --colors=always",
"cs-fix": "php-cs-fixer fix $1",
"analyse": "phpstan analyse --memory-limit 300M -l 0 -c phpstan.neon ./app ./config",
"start": [
"Composer\\Config::disableProcessTimeout",
"php ./bin/hyperf.php start"
]
}
}
view.php配置如下

<?php declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact group@hyperf.io
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
use Hyperf\View\Engine\NoneEngine;
use Hyperf\View\Mode; return [
// 使用的渲染引擎
// 'engine' => NoneEngine::class,
// 'engine' => Hyperf\ViewEngine\HyperfViewEngine::class,
'engine' => think\TemplateEngine::class,
// 不填写则默认为 Task 模式,推荐使用 Task 模式
'mode' => Mode::TASK,
'config' => [
'view_path' => BASE_PATH . '/storage/view/',
'cache_path' => BASE_PATH . '/runtime/view/',
],
];
server.php配置如下

<?php declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact group@hyperf.io
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
use Hyperf\Server\Event;
use Hyperf\Server\Server;
use Swoole\Constant; return [
'mode' => SWOOLE_PROCESS,
'servers' => [
[
'name' => 'http',
'type' => Server::SERVER_HTTP,
'host' => '0.0.0.0',
'port' => 9501,
'sock_type' => SWOOLE_SOCK_TCP,
'callbacks' => [
Event::ON_REQUEST => [Hyperf\HttpServer\Server::class, 'onRequest'],
],
],
],
'settings' => [
Constant::OPTION_ENABLE_COROUTINE => true,
Constant::OPTION_WORKER_NUM => swoole_cpu_num(),
Constant::OPTION_PID_FILE => BASE_PATH . '/runtime/hyperf.pid',
Constant::OPTION_OPEN_TCP_NODELAY => true,
Constant::OPTION_MAX_COROUTINE => 100000,
Constant::OPTION_OPEN_HTTP2_PROTOCOL => true,
Constant::OPTION_MAX_REQUEST => 100000,
Constant::OPTION_SOCKET_BUFFER_SIZE => 2 * 1024 * 1024,
Constant::OPTION_BUFFER_OUTPUT_SIZE => 2 * 1024 * 1024,
'task_worker_num' => 8,// Task Worker 数量,根据您的服务器配置而配置适当的数量
'task_enable_coroutine' => false,// 因为 `Task` 主要处理无法协程化的方法,所以这里推荐设为 `false`,避免协程下出现数据混淆的情况
// 静态资源
'document_root' => BASE_PATH . '/static',
'enable_static_handler' => true,
],
'callbacks' => [
Event::ON_WORKER_START => [Hyperf\Framework\Bootstrap\WorkerStartCallback::class, 'onWorkerStart'],
Event::ON_PIPE_MESSAGE => [Hyperf\Framework\Bootstrap\PipeMessageCallback::class, 'onPipeMessage'],
Event::ON_WORKER_EXIT => [Hyperf\Framework\Bootstrap\WorkerExitCallback::class, 'onWorkerExit'],
Event::ON_TASK => [Hyperf\Framework\Bootstrap\TaskCallback::class, 'onTask'],
Event::ON_FINISH => [Hyperf\Framework\Bootstrap\FinishCallback::class, 'onFinish'],
],
];
建立文件夹
<?php declare(strict_types=1); namespace App\Controller; use Hyperf\HttpServer\Annotation\AutoController;
use Hyperf\View\RenderInterface; /**
* @AutoController
*/
class ViewController
{
public function index(RenderInterface $render)
{
return $render->render('view_index', ['name' => 'Hyperf']);
}
}
路由config/routes.php再加一下
Router::addRoute(['GET', 'POST', 'HEAD'], '/view/', 'App\Controller\ViewController::index');
访问http://域名/view就能加载视图模板了
访问静态文件的话需要在static/下面放个index.js

启动:
php bin/hyperf.php start
这样就能访问了
另外这里提一句如何使用nginx作为服务器转发local.hyperf.com给swoole运行
来到/etc/nginx/sites-available 创建文件local.hyperf.com
内容如下:
# 至少需要一个 Hyperf 节点,多个配置多行
upstream hyperf {
# Hyperf HTTP Server 的 IP 及 端口
server 127.0.0.1:9501;
}
server {
# 监听端口
listen 80;
# 绑定的域名,填写您的域名
server_name local.hyperf.com;
location / {
# 将客户端的 Host 和 IP 信息一并转发到对应节点
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# 转发Cookie,设置 SameSite
proxy_cookie_path / "/; secure; HttpOnly; SameSite=strict";
# 执行代理访问真实服务器
proxy_pass http://127.0.0.1:9501;
}
}
然后来到/etc/nginx/sites-enabled
创建软链接
sudo ln -s ../sites-available/local.hyperf.com local.hyperf.com
重启nginx
别忘了改一下hosts文件在/etc/hosts
127.0.0.1 local.hyperf.com
结束后就可以访问了 http://local.hyperf.com/
hyperf的使用的更多相关文章
- Docker 下开发安装hyperf
Docker 下开发hyperf # 下载并运行 hyperf/hyperf 镜像,并将镜像内的项目目录绑定到宿主机的 /tmp/skeleton 目录 docker run -v /tmp/skel ...
- Hyperf基础教程
前提说明 本教程适用于新手.老手,也适用于任何操作系统,包括Windows.linux.MacOS 介绍 Hyperf 是基于 Swoole 4.4+ 实现的高性能.高灵活性的 PHP 协程框架,内置 ...
- PHP-Parse 简介以及在 Hyperf 中的应用
介绍 PHP-Parse 是分析 PHP 代码生成 AST 的库,分析出可读性很高的对象数据结构,方便后续的更新和遍历. PHP-Parse 的主要作用是修改原有代码(比如插入自定义的代码片段),生成 ...
- hyperf从零开始构建微服务(一)——构建服务提供者
阅读目录 什么是服务 构建服务提供者 1.创建数据表 2.构建服务提供者 3.安装json rpc依赖 4.安装rpc server组件 5.修改server配置 6.配置数据库 7.编写基础代码 7 ...
- hyperf从零开始构建微服务(二)——构建服务消费者
阅读目录 构建服务消费者 安装json rpc依赖 安装JSON RPC客户端 server配置 编写业务代码 编写服务消费者类 consumer配置 配置 UserServiceInterface ...
- hyperf 如何对AMQP消息进行手动消费?
转发自白狼栈:查看原文 在使用 hyperf 官方自带的 AMQP 队列时你会发现,不需要我们再额外启动进程对消息进行消费.这是因为默认情况下,使用 @Consumer 注解时,hyperf 会为我们 ...
- Hyperf使用ElasticSearch记录
Hyperf 安装 Elasticsearch 协程客户端 hyperf/elasticsearch 主要为 elasticsearch-php 进行了客户端对象创建的工厂类封装,elasticsea ...
- Hyperf 接入阿里云ACM应用配置管理中心
参考: 阿里云文档:https://help.aliyun.com/document_detail/85466.html?spm=a2c4g.11186623.6.550.43cb42d4Af4Tu0 ...
- hyperf 配置 https 访问
最近用hyperf写了支付系统,本地调试支付完成,打包上线部署,要解决https协议进行相应的访问,但是hyperf 官方没有找到相关的ssl配置说明.搜了一下soole还是有几个案例说明,据我的了解 ...
- Hyperf微服务
https://hyperf.wiki 状态码含义 https://segmentfault.com/a/1190000002523655
随机推荐
- IE中在线预览PDF文件
今天在项目中偶然遇到一个需要在线查看pdf的需求.在查阅一些资料之后使用了最简单的写法(需要在客户端安装AdbeRdr11000_zh_CN_11.0.0.379.exe软件). 还有其他方法可以实现 ...
- SQL Server磁盘空间清理 【转】
SQL Server在删除数据后,会重新利用这部分空间,所以如果不是空间紧张的情况下,可以不回收.回收一般先回收日志文件,因为这个回收速度非常快,可以短时间内清理出一部分可用空间.回收步骤: 1.查看 ...
- Failed to convert value of type 'java.lang.String' to required type
DEBUG 微信小程序Java后台 Failed to convert value of type 'java.lang.String' to required type 产生这种条件的原因一般是使用 ...
- 1Panel:一个现代化、开源的 Linux 服务器运维管理面板
前言 之前有小伙伴问:Linux 服务器运维管理除了宝塔,还有其他值得推荐的管理软件吗?,今天大姚给大家分享一个现代化.开源的 Linux 服务器运维管理面板:1Panel. 项目介绍 1Panel是 ...
- Dart 2.12 现已发布
作者 / Michael Thomsen Dart 2.12 现已发布,其中包含 健全的空安全 和 Dart FFI 的稳定版.空安全是我们最新主打的一项生产力强化功能,意在帮助您规避空值错误,以前这 ...
- 暑假集训CSP提高模拟18
\[暑假集训CSP提高模拟 \ 1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1 \] Very good problem, this make my news rotate. ...
- 深入理解 Nuxt.js 中的 app:created 钩子
title: 深入理解 Nuxt 中的 app created 钩子 date: 2024/9/26 updated: 2024/9/26 author: cmdragon excerpt: 摘要:本 ...
- Springboot 项目配置 HTTPS
生成证书 输入命令 keytool -genkeypair -alias "boot" -keyalg "RSA" -keystore "boot.k ...
- 揭秘!尤雨溪成立的VoidZero如何改变前端世界
前言 Vue和Vite之父尤雨溪宣布成立公司 VoidZero,目前已经融资3200万.这篇文章欧阳将带你了解VoidZero是如何改变javascript的世界! 关注公众号:[前端欧阳],给自己一 ...
- USB总线-Linux内核USB3.0主机控制器驱动框架分析(十二)
1.概述 如下图所示,Linux内核中USB主机体系结构由五部分组成,分别为Application Software.USB Class Driver.USB Core((USB Driver).US ...