发布于 2015-08-09 08:54:37 | 1143 次阅读 | 评论: 0 | 来源: 网络整理

Swoole框架提供的WebServer有3种使用方法

一、直接使用HttpServer

HttpServer支持静态文件和include file。业务代码不需要写任何Server的代码,只需要设置document_root,并编写对应php文件。这种使用方法与Apache/Nginx+FPM类似。

server.php

$AppSvr = new SwooleNetworkProtocolHttpServer();
$AppSvr->loadSetting("./swoole.ini"); //加载配置文件
$AppSvr->setDocumentRoot(__DIR__.'/webdocs/'); //设置document_root

$server = new SwooleNetworkServer('0.0.0.0', 9501);
$server->setProtocol($AppSvr);
//$server->daemonize(); //作为守护进程
$server->run(array('worker_num' => 2, 'max_request' => 1000));

webdocs/index.php

<?php
echo "hello world";

在浏览器中打开http://localhost:9501/index.php

二、继承HttpServer

业务代码只需要继承此类,并自行实现onRequest方法即可。

    /**
     * 处理请求
     * @param $request
     * @return SwooleResponse
     */
    function onRequest(SwooleRequest $request)

onRequest方法参数为解析好的Request对象

  • $request->post : $_POST
  • $request->get : $_GET
  • $request->cookie : $_COOKIES
  • $request->file $_FILES

onRequest方法必须返回一个Response对象

  • $response->body 返回的HTML内容
  • $response->head HTTP头信息

三、使用AppServer

基于AppServer类开发,就必须遵循Swoole MVC规范。具体可以查看examples/和apps/中的示例程序。 apps/目录中存放应用代码。

目录 说明
apps/controllers 控制器代码
apps/models 数据模型代码
apps/teamplets 模板文件
apps/config 配置文件
最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务