使用easyswoole进行开发web网站(1)

简介: 使用easyswoole进行开发web网站

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

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

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

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

查看文档以及demo

也可查看最新文章:easyswoole快速实现一个网站的api接口程序

一:使用nginx代理easyswoole  http

nginx增加配置:

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

如图:

image.png

image.png

三:引入自定义配置

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

image.png

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

<?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:调用方法:

// 获得配置
    $Conf = Config::getInstance()->getConf(文件名);

四:使用ThinkORM

1:安装

composer require topthink/think-orm

2:创建配置文件

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

<?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参照以下代码

<?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查询一样

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

namespace App\\Model;
use App\\Base\\Tool;
use EasySwoole\\Core\\AbstractInterface\\Singleton;
use think\\Db;
use think\\Model;
Class user extends Model
{
}

即可使用model

use App\\Model\\User;
 function index(){
    $member = User::get(1);
    $member->username = 'test';
    $member->save();
    $this->response()->write('Ok');}

五:使用tp模板引擎

1:安装

composer require topthink/think-template

2:建立view基类

<?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类

<?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

test:{$test}<br>

即可使用模板引擎

目录
相关文章
|
2月前
|
算法 Java Go
【GoGin】(1)上手Go Gin 基于Go语言开发的Web框架,本文介绍了各种路由的配置信息;包含各场景下请求参数的基本传入接收
gin 框架中采用的路优酷是基于httprouter做的是一个高性能的 HTTP 请求路由器,适用于 Go 语言。它的设计目标是提供高效的路由匹配和低内存占用,特别适合需要高性能和简单路由的应用场景。
244 4
|
6月前
|
缓存 JavaScript 前端开发
鸿蒙5开发宝藏案例分享---Web开发优化案例分享
本文深入解读鸿蒙官方文档中的 `ArkWeb` 性能优化技巧,从预启动进程到预渲染,涵盖预下载、预连接、预取POST等八大优化策略。通过代码示例详解如何提升Web页面加载速度,助你打造流畅的HarmonyOS应用体验。内容实用,按需选用,让H5页面快到飞起!
|
6月前
|
JavaScript 前端开发 API
鸿蒙5开发宝藏案例分享---Web加载时延优化解析
本文深入解析了鸿蒙开发中Web加载完成时延的优化技巧,结合官方案例与实际代码,助你提升性能。核心内容包括:使用DevEco Profiler和DevTools定位瓶颈、四大优化方向(资源合并、接口预取、图片懒加载、任务拆解)及高频手段总结。同时提供性能优化黄金准则,如首屏资源控制在300KB内、关键接口响应≤200ms等,帮助开发者实现丝般流畅体验。
|
前端开发 JavaScript Shell
鸿蒙5开发宝藏案例分享---Web页面内点击响应时延分析
本文为鸿蒙开发者整理了Web性能优化的实战案例解析,结合官方文档深度扩展。内容涵盖点击响应时延核心指标(≤100ms)、性能分析工具链(如DevTools时间线、ArkUI Trace抓取)以及高频优化场景,包括递归函数优化、网络请求阻塞解决方案和setTimeout滥用问题等。同时提供进阶技巧,如首帧加速、透明动画陷阱规避及Web组件初始化加速,并通过优化前后Trace对比展示成果。最后总结了快速定位问题的方法与开发建议,助力开发者提升Web应用性能。
|
6月前
|
JSON 开发框架 自然语言处理
【HarmonyOS Next之旅】基于ArkTS开发(三) -> 兼容JS的类Web开发(三)
本文主要介绍了应用开发中的三大核心内容:生命周期管理、资源限定与访问以及多语言支持。在生命周期部分,详细说明了应用和页面的生命周期函数及其触发时机,帮助开发者更好地掌控应用状态变化。资源限定与访问章节,则聚焦于资源限定词的定义、命名规则及匹配逻辑,并阐述了如何通过 `$r` 引用 JS 模块内的资源。最后,多语言支持部分讲解了如何通过 JSON 文件定义多语言资源,使用 `$t` 和 `$tc` 方法实现简单格式化与单复数格式化,为全球化应用提供便利。
257 104
|
设计模式 Web App开发 存储
移动 Web 开发的10个优秀 JavaScript 框架
选择正确的 JavaScript 框架,对于开发移动 Web 应用程序是至关重要的,也是移动应用程序开发的一项重要任务。开发人员可以使用框架实现的功能高效地达到他们的开发目标。这些预实现的组件采用优秀的设计模式和最佳实践,促进应用程序以标准化的方式开发。最重要的是,它让开人员在开发过程中得心应手。
760 0
移动 Web 开发的10个优秀 JavaScript 框架
|
Web App开发 移动开发 JavaScript
【今日推荐】移动 Web 开发的10个最佳 JavaScript 框架
  选择正确的 JavaScript 框架,对于开发移动 Web 应用程序是至关重要的,也是移动应用程序开发的一项重要任务。开发人员可以使用框架实现的功能高效地达到他们的开发目标。这些预实现的组件采用优秀的设计模式和最佳实践,促进应用程序以标准化的方式开发。
2072 0
|
6月前
|
JavaScript 前端开发 API
【HarmonyOS Next之旅】基于ArkTS开发(三) -> 兼容JS的类Web开发(二)
本文介绍了HarmonyOS应用开发中的HML、CSS和JS语法。HML作为标记语言,支持数据绑定、事件处理、列表渲染等功能;CSS用于样式定义,涵盖尺寸单位、样式导入、选择器及伪类等特性;JS实现业务逻辑,包括ES6语法支持、对象属性、数据方法及事件处理。通过具体代码示例,详细解析了页面构建与交互的实现方式,为开发者提供全面的技术指导。
278 104
|
6月前
|
开发框架 编解码 JavaScript
【HarmonyOS Next之旅】基于ArkTS开发(三) -> 兼容JS的类Web开发(一)
该文档详细介绍了一个兼容JS的类Web开发范式的方舟开发框架,涵盖概述、文件组织、js标签配置及app.js等内容。框架采用HML、CSS、JavaScript三段式开发方式,支持单向数据绑定,适合中小型应用开发。文件组织部分说明了目录结构、访问规则和媒体文件格式;js标签配置包括实例名称、页面路由和窗口样式信息;app.js则描述了应用生命周期与对象管理。整体内容旨在帮助开发者快速构建基于方舟框架的应用程序。
276 102
|
7月前
|
Web App开发 前端开发 JavaScript
鸿蒙5开发宝藏案例分享---Web适配一多开发实践
这是一份实用的鸿蒙Web多设备适配开发指南,针对开发者在不同屏幕尺寸下的布局难题提供了解决方案。文章通过三大法宝(相对单位、媒体查询和窗口监听)详细介绍如何实现智能适配,并提供了多个实战案例,如宫格布局、对话框变形和自适应轮播图等。此外,还分享了调试技巧及工具推荐,帮助开发者快速上手并优化性能。最后鼓励读者实践探索,并提示更多官方资源等待发现。

热门文章

最新文章