基于workerman做最简单的访问route实现简单的mvc框架之一(原创)

46 篇文章 0 订阅
15 篇文章 0 订阅
本文介绍了如何基于Workerman在PHP环境中构建高性能的WebAPI,通过http_route4.php和workerman的配置,实现了简单的MVC路由。通过访问不同的URL路径,可以触发不同的控制器方法,例如访问'/user/'会调用'user'控制器的'index'方法,返回相应信息。
摘要由CSDN通过智能技术生成

近期需要做一些高性能webapi方面的尝试,发现市场上目前基于php主要有两种方案,swoole和workerman。

swoole是基于扩展,基于linux环境的高性能方案,其有现现成的服务框架如swoft等。

而workerman基于纯原生php,可基于linux或者windows或其他能直接运行php的环境,其也有现成的mvc web框架如webman。

本次主要基于workerman进行一些mvc的尝试。

测试目录如下:

workerman下有文件http_route4.php、http_route4.bat

bat文件中为:start php.exe http_route4.php

运行后即可监控起来:

inc里面有文件para.php,内容如下:

<?php
$root_path = $_SERVER['DOCUMENT_ROOT'];
$php_self = $_SERVER['PHP_SELF'];
$php_file_path=__FILE__;
if(file_exists("d:/wamp/www/rain_inc/"))set_include_path("d:/wamp/www/rain_inc/");
if(file_exists("/myweb/wwwroot/rain_inc/"))set_include_path("/myweb/wwwroot/rain_inc/");

http_route4.php主要内容如下:

<?php
chdir(dirname(__FILE__));//先切换到正确的目录
include("../inc/para.php");
require_once("vendor/autoload.php");
use Workerman\Worker;
use Workerman\Protocols\Http\Response;
//require_once './Workerman/Autoloader.php';
define('APPPATH',__dir__."/");
$worker = new Worker('http://0.0.0.0:8888');
$i=0;
$worker->onMessage = function($connection, $request)
{
    global $i;
    $i++;
    $mvc_urls=explode("/",$request->path());
    
    //$mvc_url_note="adasdf";
    $mvc_len=count($mvc_urls);
    $c="app";
    $a="index";
    if($mvc_len>1){
        if($mvc_len==2&&!empty($mvc_urls[0])){$c=!empty($mvc_urls[0])?$mvc_urls[0]:"app";$a=!empty($mvc_urls[1])?$mvc_urls[0]:"index";}
        if($mvc_len==3&&empty($mvc_urls[0])){$c=!empty($mvc_urls[1])?$mvc_urls[1]:"app";$a=!empty($mvc_urls[2])?$mvc_urls[2]:"index";}
        $request->session()->set("aaa",2222);
        if(file_exists("app/controllers/".$c.".php")){
        include_once("app/controllers/".$c.".php");
        $cls=new $c();
        if(method_exists($c,$a)){
            $return=$cls->$a($request);
            }else{
                $return="module:".$c." action:".$a." 404 can't found!";
                }
        $connection->send(json_encode($return));
        }else
        {
            $connection->send("module file not found 404 error!");
        }
    }else
    {
        print_r($mvc_urls);
        $connection->send("module not found mvc_len error!");
    }

};

// 运行worker
Worker::runAll();

控制器文件user.php内容如下:

<?php
// include_once("system/core.php");load_page();

class user{
    function index($request){
        $depts=array("code"=>200,"msg"=>"index success!");
        //echo json_encode($depts);
        return ($depts);
    }
    function go($request){
        $depts=array("code"=>200,"msg"=>"module user go success!","module"=>$request->sessionId(),"aaa"=>$request->session()->get("aaa"),"get"=>$request->get("a"));
        return ($depts);
        //return mvc_display($depts,"app","user","go");
    }
}

实现的效果如下:

如果访问:http://localhost:8888/user/

则显示:

如果访问:http://localhost:8888/user/go,则显示

已实现简单的访问路由。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lwprain

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值