Swoft2.x 小白学习笔记 (一) ---控制器
Swoft通过官方文档进行学习,这里不做介绍,直接上手。
涉及到Swoft方面:(配置、注意的坑)
1、控制器(路由、验证器、中间件)
4、协程简单实现
5、RPC实现
准备: 1、先安装php各种扩充,有的扩充不兼容需要禁用
2、先安装 swoftcli,能让修改代码重新启动,生效。https://www.swoft.org/docs/2.x/zh-CN/tool/swoftcli/index.html
一、Http/Https服务控制器
1、配置,在 /app/bean.php中
'httpServer' => [
'class' => HttpServer::class,
'port' => 18306,
'listener' => [
],
'process' => [
],
'on' => [
],
// 'type' => SWOOLE_SOCK_TCP | SWOOLE_SSL, //支持https,必须安装OpenSSL扩充
/* @see HttpServer::$setting */
'setting' => [0
'worker_num' => 4,
//支持https,签名文件
// 'ssl_cert_file' => '/my/certs/2288803_www.domain.com.pem',
// 'ssl_key_file' => '/my/certs/2288803_www.domain.com.key',
]
],
2、新建控制器,在 /app/Http/Controller/文件夹下新建文件 IndexController.php
/**
* Class IndexController
* @package App\Http\Controller
* @Controller(prefix="/apidemo")
*/
class IndexController{
/**
* @RequestMapping("index",method={RequestMethod::GET})
* @throws \Swoft\Exception\SwoftException
*/
public function index(){
$res = Context()->getResponse(); $data = ['name'=>'Swoft2.0.2222'];
return $res->withData($data);
} /**
* @RequestMapping("index_v2")
* @throws \Swoft\Exception\SwoftException
*/
public function indexV2(){
$res = Context()->getResponse(); $data = ['name'=>'Swoft2.0.2222'];
return $res->withData($data);
}
}
注意事项:1、Swoft路径全部使用注解方式进行。注解必须使用 /** 开始,不能少或者多 * ,只能是两个* ,不然会报错。
2、如果有其他注释,不能出现@符合,如下:
/**
*
* //路径解析
* //将此注解应用于 Action 上,则作用域仅为当前的 Action @Middleware 用于配置单个中间件 @Middlebrows 用于配置一组 Middleware
* * @RequestMapping("demo_middle")
*/ 会报错,蓝色部分是你的注释,其中 @Middleware 不能加@符号,不然会报错。 3、路由访问是通过 @Controller(prefix="/apidemo") 中的prefix + 类中每个方法的@RequestMapping("index") 参数:url/apidemo/index 进行访问当前控制器的方法。
4、如果有多个方法的路由相同,会以最后一个为准,覆盖之前的。 详细路由查看:https://www.swoft.org/docs/2.x/zh-CN/http-server/route.html
3、启动 swoftcli run -c http:start -b bin/swoft
浏览器访问 http://127.0.0.1:18306/apidemo/index
二:验证器:https://www.swoft.org/docs/2.x/zh-CN/validator/index.html
1、安装组件 composer require swoft/validator
2、配置
'httpDispatcher' => [
// Add global http middleware
'afterMiddlewares' => [
\Swoft\Http\Server\Middleware\ValidatorMiddleware::class
]
],
3、在文件夹 /app/Validator/下新建文件DemoValidator.php
<?php declare(strict_types=1); namespace App\Validator;
use Swoft\Validator\Annotation\Mapping\Validator;
use Swoft\Validator\Contract\ValidatorInterface;
use Swoft\Validator\Exception\ValidatorException;
/**
* Class DemoValidator
*
* @since 2.0
*
* @Validator(name="demoValidator") //给验证器取个名字
*/
class DemoValidator implements ValidatorInterface
{
/**
* @return array
* @throws ValidatorException
*/
public function validate(array $data, array $params): array
{
$start = $data['start'] ?? null;
$end = $data['end'] ?? null;
if ($start === null && $end === null) {
throw new ValidatorException('Start time and end time cannot be empty');
}
if ($start > $end) {
throw new ValidatorException('Start cannot be greater than the end time');
}
return $data;
}
}
4、使用。在前面新建的控制器中
/**
* 访问: http://127.0.0.1:18306/apidemo/valida_v2?start=4&end=2
*
* @RequestMapping("valida_v2") //路由
* @Validate(validator="demoValidator",type=ValidateType::GET) //使用验证器,默认是post验证,加上type修改验证请求方式
* @throws \Swoft\Exception\SwoftException
*/
public function validaV2Controller(){
$res = context()->getResponse();
$data = ['name'=>'Swoft2.0.2222']; return $res->withData($data);
}
访问: http://127.0.0.1:18306/apidemo/valida_v2?start=4&end=8 ,不加参数或者end比start值大都会抛出异常
三、中间件:https://www.swoft.org/docs/2.x/zh-CN/http-server/middleware.html
1、定义,在文件夹 /app/Http/Middleware/ 下新建文件DemoMiddleware.php
只需要实现了 Swoft\Http\Server\Contract\MiddlewareInterface 接口均为一个合法的中间件,其中 process() 方法为该中间件逻辑处理方法 <?php declare(strict_types=1); namespace App\Http\Middleware; use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Swoft\Bean\Annotation\Mapping\Bean;
use Swoft\Http\Server\Contract\MiddlewareInterface; /**
* //中间件必须实现 MiddlewareInterface 接口
* @Bean()
*/
class DemoMiddleware implements MiddlewareInterface{
/**
* @return ResponseInterface
* @inheritdoc
* @throws \Swoft\Exception\SwoftException
*/
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
// TODO: Implement process() method.
$path = $request->getUri()->getPath(); var_dump($path); $request = $handler->handle($request);
return $request; }
2、全局使用,当中间件需要全局使用时,直接在bean中配置。否则使用步骤3.
'httpDispatcher' => [
// Add global http middleware
'middlewares' => [
\App\Http\Middleware\FavIconMiddleware::class,
\App\Http\Middleware\DemoMiddleware::class //添加新的中间件,添加后会让所有控制器都会默认使用该控制器
],
'afterMiddlewares' => [
\Swoft\Http\Server\Middleware\ValidatorMiddleware::class
]
],
3、不进行全局使用时,在需要的地方进行注解使用。在前面创建的控制器中。
use App\Http\Middleware\DemoMiddleware; //先在类最上面引用
/**
*
* //中间间测试用
* //当将此注解应用于 Controller 上,则作用域为整个 Controller
* //将此注解应用于 Action 上,则作用域仅为当前的 Action Middleware 用于配置单个中间件 Middlebrows 显而易见的是用于配置一组 Middleware,按照定义顺序依次执行
*
* @RequestMapping("demo_middle")
*
* @Middleware(DemoMiddleware::class) //通过注解加入中间件,只有当前路由会使用到该中间件
*/
public function demoMiddle(){
return "dddd";
}
参考文档 :
源码解析:https://www.jianshu.com/p/2f679e0b4d58
Swoft2.x 小白学习笔记 (一) ---控制器的更多相关文章
- Swoft2.x 小白学习笔记 (四) --- RPC
介绍 swoft 中 RPC使用:搭建访问服务端和客户端 RPC服务端: 一.配置,在文件 /app/bean.php中添加 return [ 'rpcServer' => [ 'class' ...
- Swoft2.x 小白学习笔记 (二) --- mysql、redis
介绍swoft中 1.mysql. 2.Redis 一.mysql使用: 1.配置,在 app\bean.php文件中 'db' => [ 'class' => Database::cla ...
- Swoft2.x 小白学习笔记 (三) --- Task、协程
介绍swoft中 1.Task 2.协程 一:Task任务: 1.配置,在 app/bean.php文件中加入 'httpServer' => [ // ... 'on' => [ Swo ...
- ThinkPHP 学习笔记 ( 二 ) 控制器 ( Controller )
/** * ThinkPHP version 3.1.3 * 部署方式:应用部署 * 文内的 http://localhost/ 由实际主机地址代替 */ 入口文件 index.php: <?p ...
- IOS 学习笔记 2015-04-15 控制器数据反向传值
// // FirstViewController.h // 控制器数据传递 // // Created by wangtouwang on 15/4/15. // Copyright (c) 201 ...
- STM32学习笔记——DMA控制器(向原子哥学习)
一.DMA简介 DMA,全称为:Direct Memory Access,即直接存储器访问,DMA 用来提供在外设和存储器之间或者存储器和存储器之间的高速数据传输.当 CPU 初始化这个传输动作,传输 ...
- ASP.NET MVC5 学习笔记-1 控制器、路由、返回类型、选择器、过滤器
[TOC] 1. Action 1.1 新建项目 新建项目->Web->Asp.net Web应用程序,选择MVC,选择添加测试. 在解决方案上右键,选择"管理NuGet程序包& ...
- Symfony2 学习笔记之控制器
一个controller是你创建的一个PHP函数,它接收HTTP请求(request)并创建和返回一个HTTP回复(Response).回复对象(Response)可以是一个HTML页面,一个XML文 ...
- ------------------java collection 集合学习 ----小白学习笔记,,有错,请指出谢谢
<!doctype html>java对象集合学习记录 figure:first-child { margin-top: -20px; } #write ol, #write ul { p ...
随机推荐
- Friend-Graph (HDU 6152)2017中国大学生程序设计竞赛 - 网络选拔赛
Problem Description It is well known that small groups are not conducive of the development of a tea ...
- BZOJ1005--[HNOI2008]明明的烦恼(树的prufer编码)
1005: [HNOI2008]明明的烦恼 Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 5768 Solved: 2253[Submit][Stat ...
- P2089 烤鸡
题目背景 猪猪hanke得到了一只鸡 题目描述 猪猪Hanke特别喜欢吃烤鸡(本是同畜牲,相煎何太急!)Hanke吃鸡很特别,为什么特别呢?因为他有10种配料(芥末.孜然等),每种配料可以放1—3克, ...
- 黑马vue---18、v-for指令的四种使用方式
黑马vue---18.v-for指令的四种使用方式 一.总结 一句话总结: (item, i) in list:什么in什么的形式,前面是各种参数 1.v-for循环普通数组? <p v-for ...
- Android ROM适配
Android是开源的,不同的手机厂商都有自己定制的系统,所以这就给开发者带来了ROM适配难题.在一些群里面经常看到有人因为手机适配问题,说这个手机坑,那个手机坑,其实那是没有对ROM定制系统的一些变 ...
- UML建模综述
一.概念 UML-Unified Model Language 统一建模语言,又称标准建模语言.是用来对软件密集系统进行可视化建模的一种语言.作为一个支持模型化和软件系统开发的图形化语言,UML为软件 ...
- vscode 记住git用户密码
一.选中文件-->首选项-->设置,搜索git,在setting.json中添加 "git.path":"/bin/git.exe" 二.git b ...
- Elasticsearch的安装入门
大纲: 一.简介 二.Logstash 三.Redis 四.Elasticsearch 五.Kinaba 一.简介 1.核心组成 ELK由Elasticsearch.Logstash和Kibana三部 ...
- Fragment 和Activity的数据传递实例代码
package com.example.fragment; import android.os.Bundle; import android.support.v4.app.FragmentActivi ...
- [Python]正则匹配字符串 | 蒲公英二维码图片url
代码示例: import re def Find(string): # findall() 查找匹配正则表达式的字符串 url = re.findall('http[s]?://(?:[a-zA-Z] ...