easyswoole作为swoole入门最简单的框架,其框架的定义就是适合大众php,更好的利用swoole扩展进行开发,

以下是本人使用easyswoole,看easyswoole文档总结出来的,关于easyswoole开发普通web网站的一些步骤

看下文之前,请先安装easyswoole框架

本文适用于es2.x版本,现在es3.x版本已经完全稳定,文档,demo完善,可移步www.easyswoole.com查看文档以及demo

一:使用nginx代理easyswoole  http

nginx增加配置:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

server {

    root /data/wwwroot/;

    server_name local.easyswoole.com;

    location / {

        proxy_http_version 1.1;

        proxy_set_header Connection "keep-alive";

        proxy_set_header X-Real-IP $remote_addr;

        if (!-e $request_filename) {

             proxy_pass http://127.0.0.1:9501;

        }

        if (!-f $request_filename) {

             proxy_pass http://127.0.0.1:9501;

        }

    }

}

二:使用nginx访问静态文件

只需要在easyswoole根目录下增加一个Public文件夹,访问时,只需要访问域名/Public/xx.css

如图:


三:引入自定义配置

1: 在App/Config/下增加database.php,web.php,config.php

2:在全局配置文件EasySwooleEvent.php中参照以下代码:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

<?php

namespace EasySwoole;

use \EasySwoole\Core\AbstractInterface\EventInterface;

use EasySwoole\Core\Utility\File;

Class EasySwooleEvent implements EventInterface

{

    public static function frameInitialize(): void

    {

        date_default_timezone_set('Asia/Shanghai');

        // 载入项目 Conf 文件夹中所有的配置文件

        self::loadConf(EASYSWOOLE_ROOT . '/Conf');

    }

    public static function loadConf($ConfPath)

    {

        $Conf  = Config::getInstance();

        $files = File::scanDir($ConfPath);

        if (!is_array($files)) {

            return;

        }

        foreach ($files as $file) {

            $data require_once $file;

            $Conf->setConf(strtolower(basename($file'.php')), (array)$data);

        }

    }

}

3:调用方法:

1

2

    // 获得配置

    $Conf = Config::getInstance()->getConf(文件名);

四:使用ThinkORM

1:安装

1

composer require topthink/think-orm

2:创建配置文件

在App/Config/database.php增加以下配置:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

<?php

/**

 * Created by PhpStorm.

 * User: tioncico

 * Date: 2018/7/19

 * Time: 17:53

 */

return [

    'resultset_type' => '\think\Collection',

    // 数据库类型

    'type' => 'mysql',

    // 服务器地址

    'hostname' => '127.0.0.1',

    // 数据库名

    'database' => 'test',

    // 用户名

    'username' => 'root',

    // 密码

    'password' => 'root',

    // 端口

    'hostport' => '3306',

    // 数据库表前缀

    'prefix' => 'xsk_',

    // 是否需要断线重连

    'break_reconnect' => true,

];

3:在EasySwooleEvent.php参照以下代码

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

<?php

namespace EasySwoole;

use \EasySwoole\Core\AbstractInterface\EventInterface;

use EasySwoole\Core\Utility\File;

Class EasySwooleEvent implements EventInterface

{

    public static function frameInitialize(): void

    {

        date_default_timezone_set('Asia/Shanghai');

        // 载入项目 Conf 文件夹中所有的配置文件

        self::loadConf(EASYSWOOLE_ROOT . '/Conf');

        self::loadDB();

    }

    public static function loadDB()

    {

        // 获得数据库配置

        $dbConf = Config::getInstance()->getConf('database');

        // 全局初始化

        Db::setConfig($dbConf);

    }

}

4:查询实例

和thinkphp5查询一样

1

2

3

4

5

6

7

8

9

10

11

Db::table('user')

    ->data(['name'=>'thinkphp','email'=>'thinkphp@qq.com'])

    ->insert();    Db::table('user')->find();Db::table('user')

    ->where('id','>',10)

    ->order('id','desc')

    ->limit(10)

    ->select();Db::table('user')

    ->where('id',10)

    ->update(['name'=>'test']);    Db::table('user')

    ->where('id',10)

    ->delete();

5:Model

只需要继承think\Model类,在App/Model/下新增User.php

1

2

3

4

5

6

7

8

9

10

11

namespace App\Model;

use App\Base\Tool;

use EasySwoole\Core\AbstractInterface\Singleton;

use think\Db;

use think\Model;

Class user extends Model

{

  

}

即可使用model

1

2

3

4

5

6

 use App\Model\User;

 function index(){

    $member = User::get(1);

    $member->username = 'test';

    $member->save();

    $this->response()->write('Ok');}

五:使用tp模板引擎

1:安装

1

composer require topthink/think-template

2:建立view基类

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

<?php

namespace App\Base;

use EasySwoole\Config;

use EasySwoole\Core\Http\AbstractInterface\Controller;

use EasySwoole\Core\Http\Request;

use EasySwoole\Core\Http\Response;

use EasySwoole\Core\Http\Session\Session;

use think\Template;

/**

 * 视图控制器

 * Class ViewController

 * @author  : evalor <master@evalor.cn>

 * @package App

 */

abstract class ViewController extends Controller

{

    private $view;

    

    /**

     * 初始化模板引擎

     * ViewController constructor.

     * @param string   $actionName

     * @param Request  $request

     * @param Response $response

     */

    public function __construct(string $actionName, Request $request, Response $response)

    {

        $this->init($actionName$request$response);

//        var_dump($this->view);

        parent::__construct($actionName$request$response);

    }

    

    /**

     * 输出模板到页面

     * @param  string|null $template 模板文件

     * @param array        $vars 模板变量值

     * @param array        $config 额外的渲染配置

     * @author : evalor <master@evalor.cn>

     */

    public function fetch($template=null, $vars = [], $config = [])

    {

        ob_start();

        $template==null&&$template=$GLOBALS['base']['ACTION_NAME'];

        $this->view->fetch($template$vars$config);

        $content = ob_get_clean();

        $this->response()->write($content);

    }

    

    /**

     * @return Template

     */

    public function getView(): Template

    {

        return $this->view;

    }

    

    public function init(string $actionName, Request $request, Response $response)

    {

        $this->view             = new Template();

        $tempPath               = Config::getInstance()->getConf('TEMP_DIR');     # 临时文件目录

        $class_name_array       explode('\\'static::class);

        $class_name_array_count count($class_name_array);

        $controller_path

                                $class_name_array[$class_name_array_count - 2] . DIRECTORY_SEPARATOR . $class_name_array[$class_name_array_count - 1] . DIRECTORY_SEPARATOR;

//        var_dump(static::class);

        $this->view->config([

                                'view_path' => EASYSWOOLE_ROOT . DIRECTORY_SEPARATOR . 'App' . DIRECTORY_SEPARATOR . 'Views' . DIRECTORY_SEPARATOR . $controller_path,

                                # 模板文件目录

                                'cache_path' => "{$tempPath}/templates_c/",               # 模板编译目录

                            ]);

        

//        var_dump(EASYSWOOLE_ROOT . DIRECTORY_SEPARATOR . 'App' . DIRECTORY_SEPARATOR . 'Views' . DIRECTORY_SEPARATOR . $controller_path);

    }

    

}

控制器继承ViewController类

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

<?php

namespace App\HttpController\Index;

use App\Base\HomeBaseController;

use App\Base\ViewController;

use EasySwoole\Core\Http\Request;

use EasySwoole\Core\Http\Response;

use think\Db;

/**

 * Class Index

 * @package App\HttpController

 */

class Index extends ViewController

{

    public function __construct(string $actionName, Request $request, Response $response)

    {

        parent::__construct($actionName$request$response);

    }

    

    protected function onRequest($action): ?bool

    {

        return parent::onRequest($action); // TODO: Change the autogenerated stub

    }

    

    public function index()

    {

        $sql "show tables";

        $re = Db::query($sql);

        var_dump($re);

        $assign array(

            'test'=>1,

            'db'=>$re

        );

        $this->getView()->assign($assign);

        $this->fetch('index');

    

    }

}

在App/Views/Index/Index/建立index.html

1

test:{$test}<br>

即可使用模板引擎

六:使用$_SESSION,$_GET,$_POST等全局变量

新增baseController控制器,继承ViewController

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

<?php

/**

 * Created by PhpStorm.

 * User: tioncico

 * Date: 2018/7/19

 * Time: 16:21

 */

namespace App\Base;

use EasySwoole\Config;

use EasySwoole\Core\Http\AbstractInterface\Controller;

use EasySwoole\Core\Http\Request;

use EasySwoole\Core\Http\Response;

use EasySwoole\Core\Http\Session\Session;

use think\Template;

class BaseController extends ViewController

{

    use Tool;

    protected $config;

    protected $session;

    public function __construct(string $actionName, Request $request, Response $response)

    {

        parent::__construct($actionName$request$response);

        $this->header();

    }

    public function defineVariable()

    {

    }

    protected function onRequest($action): ?bool

    {

        return parent::onRequest($action); // TODO: Change the autogenerated stub

    }

    public function init($actionName$request$response)

    {

        $class_name                         static::class;

        $array                              explode('\\'$class_name);

        $GLOBALS['base']['MODULE_NAME']     = $array[2];

        $GLOBALS['base']['CONTROLLER_NAME'] = $array[3];

        $GLOBALS['base']['ACTION_NAME']     = $actionName;

        $this->session($request$response)->sessionStart();

        $_SESSION['user'] = $this->session($request$response)->get('user');//session

        $_GET     $request->getQueryParams();

        $_POST    $request->getRequestParam();

        $_REQUEST $request->getRequestParam();

        $_COOKIE  $request->getCookieParams();

        $this->defineVariable();

        parent::init($actionName$request$response); // TODO: Change the autogenerated stub

        $this->getView()->assign('session'$_SESSION['user']);

    }

    public function header()

    {

        $this->response()->withHeader('Content-type''text/html;charset=utf-8');

    }

    /**

     * 首页方法

     * @author : evalor <master@evalor.cn>

     */

    public function index()

    {

        return false;

    }

    function session($request = null, $response = null): Session  //重写session方法

    {

        $request == null && $request $this->request();

        $response == null && $response $this->response();

        if ($this->session == null) {

            $this->session = new Session($request$response);

        }

        return $this->session;

    }

}

在EasySwooleEvent.php  afterAction中,进行销毁全局变量

1

2

3

4

5

6

7

8

public static function afterAction(Request $request, Response $response): void

{

    unset($GLOBALS);

    unset($_GET);

    unset($_POST);

    unset($_SESSION);

    unset($_COOKIE);

}

七:使用fastRoute自定义路由

1:在App/HttpController下新增文件Router.php

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

<?php

/**

 * Created by PhpStorm.

 * User: tioncico

 * Date: 2018/7/24

 * Time: 15:20

 */

namespace App\HttpController;

use EasySwoole\Config;

use EasySwoole\Core\Http\Request;

use EasySwoole\Core\Http\Response;

use FastRoute\RouteCollector;

class Router extends \EasySwoole\Core\Http\AbstractInterface\Router

{

    

    function register(RouteCollector $routeCollector)

    {

        $configs = Config::getInstance()->getConf('web.FAST_ROUTE_CONFIG');//直接取web配置文件的配置

        foreach ($configs as $config){

            $routeCollector->addRoute($config[0],$config[1],$config[2]);

        }

        

    }

}

web.config配置

1

2

3

4

5

6

7

8

9

10

11

12

<?php

/**

 * Created by PhpStorm.

 * User: tioncico

 * Date: 2018/7/24

 * Time: 9:03

 */

return array(

    'FAST_ROUTE_CONFIG' => array(

        array('GET','/test''/Index/Index/test'),//本人在HttpController目录下分了模块层,所以是Index/Index

    ),

);

访问xx.cn/test 即可重写到/Index/Index/test方法

八:现成源码

本文为仙士可原创文章,转载无需和我联系,但请注明来自仙士可博客www.php20.cn

https://github.com/tioncico/easyES

使用easyswoole进行开发web网站相关推荐

  1. Web发展简史((webapp+Java原生)移动端开发 )+web网站)

    Web发展简史--------->((webapp+Java原生)移动端开发(微信小程序,公众号,头条app))+web网站) Web发展简史 1:在那时,Web开发还比较简单,开发者经常会去操 ...

  2. 用Python+Django在Eclipse环境下开发web网站【转】

    一.创建一个项目 如果这是你第一次使用Django,那么你必须进行一些初始设置.也就是通过自动生成代码来建立一个Django项目--一个Django项目的设置集,包含了数据库配置.Django详细选项 ...

  3. Intellij Idea15开发Web网站

    我的环境: mac osx 10.11 intellij idea15企业版 tomcat9 jdk 1.8 目标:创建不带第三方框架的web工程,使用servlet+jdbc开发网站. 选择菜单Fi ...

  4. 使用Python-Flask框架开发Web网站系列课程(一)构建项目

    版权声明:如需转载,请注明转载地址. https://blog.csdn.net/oJohnny123/article/details/81907475 前言 使用IDE:PyCharm 操作系统: ...

  5. NodeJS学习笔记(一)——搭建开发框架Express,实现Web网站登录验证

    目录 开发环境 1.建立工程 2.目录结构 3.Express配置文件 4.Ejs模板 5.安装常用库及页面分离 6.路由 7.session 8.页面访问控制及提示 JS是脚本语言,脚本语言都需要一 ...

  6. 如何开发Web应用程序(非网站)

    看到这篇原文说,web应用程序和网站的开发不一样,收藏学习一下 这是一个经常被问到的问题,问的理所当然.作为一个程序员,为什么我就非要被认为知道如何开发Web应用程序呢?这个问题没有一个简单的答案,甚 ...

  7. c#arcgis engine开发_湖南web开发学习网站要多久

    湖南web开发学习网站要多久第13章命令模式(Command)1. 命令模式的关键命令模式的关键之处就是把请求封装成为对象,也就是命 令对象,并定义了统一的执行操作的接口,这个命令对象可以被存储.转发 ...

  8. 微信开发必备工具:利用cpolar在公网上测试本地Web网站或移动应用程序

    作为Web网站或移动应用程序的开发人员,你是否希望将NAT或防火墙后面的本地开发主机暴露到公网上,然后方便地使用公网地址进行各种测试?在本教程中,我们将教你如何使用cpolar做到这一点. cpola ...

  9. 用eclipse europa开发web service服务 - 东写西读终见大海无量 - JavaEye技术网站

    用eclipse europa开发web service服务 eclipse europa自带web工具.我们可以使他生成动态web程序.但是在默认情况下,生成的动态默认程序是不包含web servi ...

最新文章

  1. OpenAI新发现:GPT-3做小学数学题能得55分,验证胜过微调!
  2. 一个mac地址对应多个ip_一个关于IP与mac地址绑定的故障
  3. 使用Java mail发送邮件附件出现附件上产生.eml文件夹的问题及附件名.bin结尾问题...
  4. 一台计算机有64,在同一台计算机上使用带有32位和64位Altium设计软件的数据库元件库...
  5. 各国家分析之 古埃及非洲经济
  6. 手机定位和什么有关?关机后的手机还能被定位吗?
  7. mysql server远程连接_本地远程连接 MySQL server
  8. android添加hidl,android hidl
  9. 5.MySQL优化---索引优化专题
  10. 正确率、召回率和F值
  11. 2022年P气瓶充装考试模拟100题模拟考试平台操作
  12. CAD如何绘制固定面积的矩形
  13. html中鼠标冒泡泡,鼠标经过出现气泡框的简单实例
  14. SQLServer中的 dbo
  15. “凡事预则立,不预则废”?
  16. Python-算法思维4.0.1迭代算法
  17. 简述关系数据库的数据完整性规则_关系数据库的完整性简述 关系数据库完整性规则...
  18. mysql 数据库军规_MySQL 数据库开发的 36 条军规
  19. C语言:递归解决年龄问题(精细版)
  20. Linux使用rpm命令卸载软件

热门文章

  1. 【求助】使用matlab机器人工具箱求逆超出矩阵索引维度且与实际工作空间不符
  2. 庆祝胖五发射成功, 来用Python发射火箭!
  3. linux svn导入dump文件,Linux下svn安装配置及备份还原
  4. 虚拟现实 VR + 3D 可视化,打造一体化高阶管控平台
  5. MeshCollider渲染面片
  6. openGL GLSL GLSL.Refract Reflect Diffraction 反射、折射、衍射Fresnel Effect
  7. git(Auto-merging错误)解决冲突
  8. 指导教师的shooow
  9. 程序员工作三年月薪不过万,遭其他人疯狂吐槽,为何还不辞职?
  10. 新浪微博的实时数据湖建设实践