在应用下的 app/bean.php 配置 server
在应用下的 app/bean.php 配置 server,在这个文件里,你可以看到 Http Server数组里面包含了 Http Server 的基本信息。下面列举了一些简单的配置,你也可以自由组合同时提供多种服务。
'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',
]
],
'httpServer' => [
'class' => HttpServer::class,
'port' => 18306,
'listener' => [
'rpc' => bean('rpcServer')
],
'process' => [
// 'monitor' => bean(MonitorProcess::class)
// 'crontab' => bean(CrontabProcess::class)
],
'on' => [
// Enable task must task and finish event
SwooleEvent::TASK => bean(TaskListener::class),
SwooleEvent::FINISH => bean(FinishListener::class)
],
/* @see HttpServer::$setting */
'setting' => [
'task_worker_num' => 12,
'task_enable_coroutine' => true,
'worker_num' => 6,
// Enable Https
'ssl_cert_file' => '/my/certs/2288803_www.domain.com.pem',
'ssl_key_file' => '/my/certs/2288803_www.domain.com.key',
]
],
//Enable Https
'type' => SWOOLE_SOCK_TCP | SWOOLE_SSL,
可配置项用于 http server bean 配置,除了 class 其他都是 http server 的属性。
class: 指定 Http Server 的处理类
port: 指定 Http Server 的端口
listener: 指定其他一同启动的服务,添加端口服务监听,可以多个。
rpc 启动 RPC 服务
process: 启动自定义用户进程
on: 配置监听的事件
注册事件、设置对应事件的处理监听,事件触发组件调用,在任务里面使用
setting: 这里是参考 Swoole Server 配置选项
pidFile: 设置进程 pid 文件 位置,默认值 @runtime/swoft.pid
mode: 运行的模式,参考 Swoole Server 构造函数 第三个参数
type: 指定Socket的类型,支持TCP、UDP、TCP6、UDP6、UnixSocket Stream/Dgram 等 Swoole Server 构造函数 第四个参数
启用 Https 支持 注意: 你必须安装 OpenSSL 库,并且确保安装 swoole 时是启用了 ssl 选项的。同时,需要设置 ‘type’ => SWOOLE_SOCK_TCP | SWOOLE_SSL
新建控制器
新建控制器,在 /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、如果有多个方法的路由相同,会以最后一个为准,覆盖之前的。
创建控制器
- 主要通过 @Controller 注解实现,代码可以放在任意位置,但为了统一标准,建议放在 app/Http/Controller 目录下
- 使用注解 @Controller (prefix=“路由前缀”),需要引入 Controller 类
路由绑定
swoft 并没有采用配置文件的方式来配置路由,而是采用了注解,可以使用 @RequestMapping 注解添加路由
使用注解 @RequestMapping ,需要引入该类,其中相关属性如下:
route 指定路由
method 指定请求方式(GET、POST、PUT、PATCH、DELETE、OPTIONS、HEAD)
params 指定 path 变量正则匹配限制
请求对象
请求对象 Request 为 Swoft\Http\Message\Request
获取请求对象
- 通过控制器方法参数注入 (Request $request)
- 通过请求上下文获取Swoft\Context\Context::mustGet()->getRequest()
响应对象
响应对象 Response 为 Swoft\Http\Message\Response
获取响应对象
- 通过控制器方法参数注入 (Response $response)
- 通过请求上下文获取 Swoft\Context\Context::mustGet ()->getResponse ()
响应对象返回数据
由于获取请求数据内容较多,比如设置状态码,输出字符串内容,设置响应数据格式,输出数组,重定向等,后面单独讲解,暂时缺省
参数验证
永远不要相信前端传输的数据
swoft 提供系统自带的验证规则进行声明验证器以及自定义验证器的功能,当然也支持自定义验证器规则,此处缺省,后续文章会详细介绍
例子代码
<?php declare(strict_types=1);
namespace app\Http\Controller;
use Swoft\Http\Message\Request;
interface BaseController
{
public function index(Request $request);
public function show(Request $request);
public function edit(Request $request);
public function update(Request $request);
public function create();
public function store(Request $request);
public function destroy(Request $request);
}
实现类
<?php declare(strict_types=1);
namespace app\Http\Controller\Admin;
use app\Http\Controller\BaseController;
use Swoft\Http\Message\Request;
use Swoft\Http\Server\Annotation\Mapping\Controller;
use Swoft\Http\Server\Annotation\Mapping\RequestMethod;
use Swoft\Validator\Annotation\Mapping\Validate;
use app\Validator\TestValidator;
use Swoft\Context\Context;
/**
* Class TestController
* @package app\Http\Controller\Admin
* @Controller(prefix="/admin/test")
*/
class TestController implements BaseController
{
/**
* @RequestMapping(route="index",method={RequestMethod::GET})
* @Validate(validator="TestValidator",fields={"name"})
* @param Request $request
*/
public function index(Request $request){
$response = Context::mustGet()->Response();
$data = ['name'=>'Swoft2.0'];
return $response->withData($data);
}
public function show(Request $request){
}
public function edit(Request $request){
}
public function update(Request $request){
}
public function create(){
}
public function store(Request $request){
}
public function destroy(Request $request){
}
}
[原创]Swoft源码剖析-目录
woft2.x 小白学习笔记 (一) —控制器
Swoft 官方开发文档
swoft 文档
swoft 文档v1
swoft 文档v2