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'],
],
];

建立文件夹

static

storage/view/
runtime/view/
里面放你要的模板文件storage/view/index.blade.php(给blade用的)和storage/view/view_index.html(给thinkTemplate用的)
然后这个view就能生效了就能访问模板文件了
<?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的使用的更多相关文章

  1. Docker 下开发安装hyperf

    Docker 下开发hyperf # 下载并运行 hyperf/hyperf 镜像,并将镜像内的项目目录绑定到宿主机的 /tmp/skeleton 目录 docker run -v /tmp/skel ...

  2. Hyperf基础教程

    前提说明 本教程适用于新手.老手,也适用于任何操作系统,包括Windows.linux.MacOS 介绍 Hyperf 是基于 Swoole 4.4+ 实现的高性能.高灵活性的 PHP 协程框架,内置 ...

  3. PHP-Parse 简介以及在 Hyperf 中的应用

    介绍 PHP-Parse 是分析 PHP 代码生成 AST 的库,分析出可读性很高的对象数据结构,方便后续的更新和遍历. PHP-Parse 的主要作用是修改原有代码(比如插入自定义的代码片段),生成 ...

  4. hyperf从零开始构建微服务(一)——构建服务提供者

    阅读目录 什么是服务 构建服务提供者 1.创建数据表 2.构建服务提供者 3.安装json rpc依赖 4.安装rpc server组件 5.修改server配置 6.配置数据库 7.编写基础代码 7 ...

  5. hyperf从零开始构建微服务(二)——构建服务消费者

    阅读目录 构建服务消费者 安装json rpc依赖 安装JSON RPC客户端 server配置 编写业务代码 编写服务消费者类 consumer配置 配置 UserServiceInterface ...

  6. hyperf 如何对AMQP消息进行手动消费?

    转发自白狼栈:查看原文 在使用 hyperf 官方自带的 AMQP 队列时你会发现,不需要我们再额外启动进程对消息进行消费.这是因为默认情况下,使用 @Consumer 注解时,hyperf 会为我们 ...

  7. Hyperf使用ElasticSearch记录

    Hyperf 安装 Elasticsearch 协程客户端 hyperf/elasticsearch 主要为 elasticsearch-php 进行了客户端对象创建的工厂类封装,elasticsea ...

  8. Hyperf 接入阿里云ACM应用配置管理中心

    参考: 阿里云文档:https://help.aliyun.com/document_detail/85466.html?spm=a2c4g.11186623.6.550.43cb42d4Af4Tu0 ...

  9. hyperf 配置 https 访问

    最近用hyperf写了支付系统,本地调试支付完成,打包上线部署,要解决https协议进行相应的访问,但是hyperf 官方没有找到相关的ssl配置说明.搜了一下soole还是有几个案例说明,据我的了解 ...

  10. Hyperf微服务

    https://hyperf.wiki 状态码含义 https://segmentfault.com/a/1190000002523655

随机推荐

  1. GPG 用法

    GPG (GnuPG) 是一种加密工具,用于数据加密和数字签名. 密钥配置 # 生成密钥 gpg --full-generate-key # 列出密钥 gpg --list-keys # 列出公钥 g ...

  2. 淘宝订单信息获取接口API,淘宝打单发货接口

    从事电商软件开发的小伙伴,在日常开发任务中,经常会遇到一个需求,就是将淘宝店铺的订单,同步到自己的内部订单管理系统OMS中,进行淘宝打单发货操作.我介绍下如何将订单同步下来,供各位参考.(注意:所有电 ...

  3. 【YashanDB知识库】设置归档日志上限,但归档日志没自动清理,导致磁盘空间满

    问题现象 客户使用一主一备做性能压测,主备机上设置了归档日志清理上下限: ARCH_CLEAN_LOWER_THRESHOLD=12G ARCH_CLEAN_UPPER_THRESHOLD=16G 但 ...

  4. 游戏AI寻路——八叉树+A*寻路

    利用八叉树的空中寻路 你有思考过在空中如何进行寻"路"吗?来想象一个的场景:飞机从空中基地出发,要避开许多空中建筑,最终到达目的地.这种情况下的寻路是没有路面的,寻路物体的移动方向 ...

  5. Round #2022/11/26

    问题 B:染色 题目描述 有长度为 \(n\) 的一个序列,编号为 \(1\) 到 \(n\) ,现要对这些元素进行染色标记,若编号 \(i-j\) 为素数,且 \(1\le i < j \le ...

  6. ComfyUI 基础教程(五) —— 应用 IP-Adapter 实现图像风格迁移

    中秋假期,又可以玩玩 AI 了.前面介绍了 ComfyUI 的 Lora 模型以及 ControlNet,本文介绍另一个非常重要且使用的节点,IP-Adapter. 一. IP-Adapter 概念 ...

  7. 暑假集训SCP提高模拟10

    我(看着百度百科):我已经知道这场谁组的题了 CTH: 谁 我:你想想,能在模拟赛里塞四道数学题还玩邦的,还能有谁 CTH: 我不知道 我:我不知道 CTH: 我知道了 我:我知道了 我:我是 Bob ...

  8. 通过MobaXterm操作远程服务器电脑的方法

      本文介绍在Windows电脑中,下载.配置MobaXterm软件,从而连接.操作远程服务器的方法.   因为是在Windows操作系统的电脑中连接服务器,所以建议使用MobaXterm.PuTTY ...

  9. ftrace options 中的irq-info

    /sys/kernel/debug/tracing/options/irq_info 是 ftrace 中的一个选项,用于启用或禁用有关中断的详细信息的跟踪. options/irq_info 的具体 ...

  10. USB总线-Linux内核USB3.0主机控制器驱动初始化流程分析(十三)

    1.概述 RK3588有2个USB3.0 DRD控制器,2个USB2.0 Host控制器.USB3.0 DRD控制器既可以做Host,也可以做Device,向下兼容USB2.0和USB1.0.USB3 ...