# swoft **Repository Path**: lewisliang82/swoft ## Basic Information - **Project Name**: swoft - **Description**: swoft是基于swoole协程2.x的高性能PHP微服务框架,内置http服务器。框架全协程实现,性能优于传统的php-fpm模式。 - **Primary Language**: PHP - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: https://github.com/swoft-cloud/swoft - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 132 - **Created**: 2017-09-21 - **Last Updated**: 2020-12-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README
# 简介 swoft是基于swoole协程2.x的高性能PHP微服务框架,内置http服务器。框架全协程实现,性能优于传统的php-fpm模式。 - 基于swoole易扩展 - 内置http协程服务器 - MVC分层设计 - 高性能路由 - 全局容器注入 - 高性能RPC - 别名机制 - 事件机制 - 国际化(i18n) - 服务治理熔断、降级、负载、注册与发现 - 连接池Mysql、Redis、RPC - 数据库ORM - inotify自动reload - 强大的日志系统 **Future** - 连接池等待队列 - restful风格 - crontab定时任务 - 服务监控 - 日志统计分析 - 统一配置中心 # 更新记录 * ...... * 2017-08-15 重构console命令行 * 2017-08-24 重写IOC容器,新增控制器路由注解注册,不再依赖php-di。使用时,重新composer安装 * 2017-08-28 inotify自动reload * 2017-09-02 别名机制、事件机制、国际化(i18n),命名空间统一大写。 * 2017-09-19 数据库ORM # 快速入门 ## 文档 [**中文文档**](https://doc.swoft.org) QQ交流群:548173319 ## 环境要求 1. hiredis 2. composer 2. PHP7.X 3. inotify(可选) 4. Swoole2.x且开启协程和异步redis ## 安装 ### 手动安装 * clone项目 * composer install安装依赖 ### composer安装 * composer.phar create-project stelin/swoft swoft(未开代理,会有点慢) ## 配置 * 配置base.php * 设置启动参数swoft.ini ## 启动 启动服务支持HTTP和TCP同时启动,swoft.ini中配置。 **常用命令** ```php //启动服务,是否是守护进程,根据swoft.ini配置 php swoft.php start //守护进程启动,覆盖swoft.ini守护进程配置 php swoft.php start -d // 重启 php swoft.php restart // 重新加载 php swoft.php reload // 关闭服务 php swoft.php stop ``` **Swoft.ini参数** ```shell [swoft] ;;;;;;;;;;;;;;;;;;; ; About swoft.ini ; ;;;;;;;;;;;;;;;;;;; [server] pfile = '/tmp/swoft.pid'; pname = "php-swf"; [tcp] enable = 1; host = "0.0.0.0" port = 8099 type = SWOOLE_SOCK_TCP [http] host = "0.0.0.0" port = 80 model = SWOOLE_PROCESS type = SWOOLE_SOCK_TCP [setting] worker_num = 4 max_request = 10000 daemonize = 0; dispatch_mode = 2 log_file = SWOOLE_LOG_PATH ``` ## 路由器 路由解析有两种方式,注册路由和自动解析,所有路由都在routes.php中配置。建议所有路由都提前注册,或者通过注解注册,不建议使用自动路由解析。路由配置参数(base.php): ```php return [ // ... 'router' => [ 'class' => \swoft\web\Router::class, 'ignoreLastSep' => false, // 是否忽略最后一个斜杠,设置false后,/user/index和/user/index/是两个不同的路由 'tmpCacheNumber' => 1000,// 缓存路由数,最近一1000条 'matchAll' => '', // 匹配所有,所有请求都会匹配到这个uri或闭包 ], // ... ]; ``` ### 路由注册实例 ```php //匹配 GET 请求. 处理器是个闭包 Closure $router->get('/', function () { $resposne = App::getResponse(); $resposne->setResponseContent("hello"); $resposne->send(); }); // 匹配参数 'test/john' $router->get('/test/{name}', function ($arg) { echo $arg; // 'john' }, [ 'tokens' => [ 'name' => '\w+', // 添加参数匹配限制。若不添加对应的限制,将会自动设置为匹配除了'/'外的任何字符 ] ]); // 可选参数支持。匹配 'hello' 'hello/john' $router->get('/hello[/{name}]', function ($name = 'No') { echo $name; // 'john' }, [ 'tokens' => [ 'name' => '\w+', // 添加参数匹配限制 ] ]); // 匹配 POST 请求 $router->post('/user/login', function () { $request = App::getRequest(); var_dump($request->getGetParameters(), $request->getPostParameters()); }); // 匹配 GET 或者 POST $router->map(['get', 'post'], '/user/login', function () { $request = App::getRequest(); var_dump($request->getGetParameters(), $request->getPostParameters()); }); // 允许任何请求方法 $router->any('/home', function () { $resposne = RequestContext::getResponse(); $resposne->setResponseContent("hello, you request page is /home"); $resposne->send(); }); $router->any('/404', function () { $resposne = App::getResponse(); $resposne->setResponseContent("Sorry,This page not found."); $resposne->send(); }); // 路由组 $router->group('/user', function ($router) { $router->get('/', function () { $resposne = App::getResponse(); $resposne->setResponseContent("hello. you access: /user/"); $resposne->send(); }); $router->get('/index', function () { $resposne = App::getResponse(); $resposne->setResponseContent("hello. you access: /user/index"); $resposne->send(); }); }); // 通过@符号连接控制器类和方法名可以指定执行方法 $router->get('/', app\controllers\Home::class); $router->get('/index', 'app\controllers\Home@index'); $router->get('/about', 'app\controllers\Home@about'); // 访问 '/home/test' 将会执行 'app\controllers\Home::test()' $router->any('/home/{any}', app\controllers\Home::class); // 可匹配 '/home', '/home/test' 等 $router->any('/home[/{name}]', app\controllers\Home::class); // 配置 matchAll 可用于拦截所有请求,目前有如下两种方式。 //路由path 'matchAll' => '/about', //回调 'matchAll' => function () { $resposne = App::getResponse(); $resposne->setResponseContent("System Maintaining ... ..."); $resposne->send(); }, ``` ## 控制器 一个继承\swoft\web\Controller的类既是控制器,控制器有两种注解自动注册和手动注册两种方式。建议使用注解自动注册,方便简洁,维护简单。多次注册相同的路由前者会被后者覆盖。 ### 注解自动注册 注解自动注册常用到三个注解@AutoController、@Inject、@RequestMapping. > @AutoController > 已经使用@AutoController,不能再使用@Bean注解。 > @AutoController注解不需要指定bean名称,统一类为bean名称 > @AutoController()默认自动解析controller前缀,并且使用驼峰格式。 > @AutoController(prefix="/demo2")或@AutoController("/demo2")功能一样,两种使用方式。 > > @Inject > 使用和之前的一样 > > @RequestMapping > @RequestMapping(route="/index2")或@RequestMapping("/index2")功能一样两种方式使用,这种默认是支持get和post方式@RequestMapping(route="/index2", method=RequestMethod::GET)注册支持的方法 > 不使用@RequestMapping或RequestMapping()功能一样,都是默认解析action方法,以驼峰格式,注册路由。 ```php /** * 控制器demo * * @AutoController(prefix="/demo2") * * @uses DemoController * @version 2017年08月22日 * @author stelin