当前仓库属于暂停状态,部分功能使用受限,详情请查阅 仓库状态说明
1 Star 0 Fork 14

小迷彩 / linkphp
暂停

forked from liugene / linkphp
暂停
 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
Apache-2.0

 _        _              _                  
| |      | |   _   ___  | |      ___
| |  ___ | | / / /  _  \| |_   /  _  \
| | | \ \| |/ /  | |_| ||  _ \ | |_| |
| |_| |\ V |\ \  | .___/| | | || .___/
|_____| \ _' \_\ | |    | | | || |

QQ: 750688237 有兴趣的可以一起探讨,项目还需要完善

linkphp (临克) 是面向对象的轻量级常驻内存型PHP API开发框架 。

http服务器启动(常驻内存模式) [需要swoole扩展]

进入

bin\

目录,使用命令

php httpd start //启动
php httpd stop  //停止

 _        _              _                  
| |      | |   _   ___  | |      ___
| |  ___ | | / / /  _  \| |_   /  _  \
| | | \ \| |/ /  | |_| ||  _ \ | |_| |
| |_| |\ V |\ \  | .___/| | | || .___/
|_____| \ _' \_\ | |    | | | || |

[2018-06-01 15:43:12] Server    Name: linkphp-httpd
[2018-06-01 15:43:12] PHP    Version: 7.1.7
[2018-06-01 15:43:12] Swoole Version: 2.1.3
[2018-06-01 15:43:12] Listen    Address: 127.0.0.1
[2018-06-01 15:43:12] Listen    Port: 9508

第一种方式:使用linkphp提倡的以常驻内存形式启动方式,前端可以配合nginx负载均衡使用

第二种方式:传统的LNMP/LAMP方式启动,则将根目录定义到src/web目录下
将会由非内存形式启动,请求一次则会进行释放,无法使用常驻内存形式提高性能

Nginx + httpd使用


server {
    root /wwwroot/;
    server_name www.linkphp.cn;

    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:9508;
        }
    }
}

WebSocket服务端启动(常驻内存模式)


首先注册一个websocket路由

Router::ws('chat','/app/ws/Handle#ws');

在Handle控制器继承WebSocketInterface接口实现接口中的方法

use linkphp\swoole\websocket\WebSocketInterface;
use swoole_http_response;
use swoole_websocket_server;
use swoole_http_request;
use swoole_server;
use swoole_websocket_frame;

class Handle implements WebSocketInterface
{

    public function HandShake(swoole_http_request $request, swoole_http_response $response)
    {
        echo "server: handshake success with fd{$request->fd}\n";
    }

    public function open(swoole_websocket_server $svr, swoole_http_request $req)
    {
        echo "server: open success with fd{$req->fd}\n";
    }

    public function message(swoole_server $server, swoole_websocket_frame $frame)
    {
        echo "receive from {$frame->fd}:{$frame->data},opcode:{$frame->opcode},fin:{$frame->finish}\n";
        $server->push($frame->fd, "this is server");
    }

    public function close($ser, $fd)
    {
        echo "client {$fd} closed\n";
    }

}

然后进入bin目录

php ws start //启动
php ws stop  //停止

websocket相关配置在conf/bin/ws.php内

之后客户端发起 ws://127.0.0.1:9510/chat 连接将会触发注册的websocket路由

PhpRpc服务端启动(常驻内存模式)


php phprpc start //启动
php phprpc stop  //停止

        _                  
  ___  | |      ___     ___     ___     ___
/  _  \| |_   /  _  \ /  _  \ /  _  \ /  __ \
| |_| ||  _ \ | |_| | | |_| | | |_| | | | 
| .___/| | | || .___/ | .\ \. | .___/ \ .__
| |    | | | || |     | |  \ \| |      \___/

[2018-06-01 15:43:12] Server    Name: phprpc
[2018-06-01 15:43:12] PHP    Version: 7.1.7
[2018-06-01 15:43:12] Swoole Version: 2.1.3
[2018-06-01 15:43:12] Listen    Address: 127.0.0.1
[2018-06-01 15:43:12] Listen    Port: 9518

PhpRpc注册中心启动(常驻内存模式)


php phprpc_center start //启动
php phprpc_center stop  //停止

        _                  
  ___  | |      ___     ___     ___     ___
/  _  \| |_   /  _  \ /  _  \ /  _  \ /  __ \
| |_| ||  _ \ | |_| | | |_| | | |_| | | | 
| .___/| | | || .___/ | .\ \. | .___/ \ .__
| |    | | | || |     | |  \ \| |      \___/

[2018-06-01 15:43:12] Server    Name: phprpc-center
[2018-06-01 15:43:12] PHP    Version: 7.1.7
[2018-06-01 15:43:12] Swoole Version: 2.1.3
[2018-06-01 15:43:12] Listen    Address: 127.0.0.1
[2018-06-01 15:43:12] Listen    Port: 9520

使用交流

linkphp开发动态:www.linkphp.cn

###目前框架空目录都是必须创建的目录根据命名规范即可自动寻址加载相关文件

部署条件

1、PHP版本不能小于5.5版本建议7.0版本(框架作者可能会发疯强制升级到php7)

支持的服务器和数据库环境

支持Windows/Unix服务器环境 可运行于包括Apache、IIS和nginx在内的多种WEB服务器和模式 框架默认只支持Mysql数据库后期可扩展,需安装相关Pdo扩展

支持composer

请确保服务器环境支持composer

在linkphp项目根目录执行 composer install命令,安装框架执行所需的所有依赖库

使用方法

事件使用


use framework\Application;

//接收两个参数,第一个参数为标签,第二个参数可以一组类的数组也可以为类名

Application::event(
    'test',
    [
        \app\controller\main\Event::class,
        \app\controller\main\Event::class,
        \app\controller\main\Event::class
    ]
);

//事件类必须实现EventServerProvider接口中update,该方法接收EventDefinition对象参数,在update方法最后将其赋值给的变量做返回操作

use linkphp\event\EventDefinition;
use linkphp\event\EventServerProvider;

class Event implements EventServerProvider
{
    public function update(EventDefinition $definition)
    {
        dump(2);
        return $definition;
        // TODO: Implement update() method.
    }
}

//事件触发

Application::event('test');

//在不给定第二个参数时则认定为触发操作

//事件配置使用方法

//在框架的configure目录下有个event.php事件配置文件

return [
    //事件标签
    //'server' => [
        //执行事件类
    //  '\app\controller\main\Event::class';
    //]
//    'test' => [
//        '\app\controller\main\Event::class',
//    ]
];

数据库使用说明

use linkphp\db\Db;
use framework\Application;

//助手函数链式操作方法
db()->table('')->where('')->select();
db()->table('zq_user')->where('id=11')->delete();
//占位操作
db()->select('select * from test_table where id = ?',[1]);

//Db类链式操作
Db::table('')->field('')->where('')->select();
Db::table('zq_user')->where('id=11')->delete();
//占位操作
Db::select('select * from test_table where id = ?',[1]);

//Application类操作
Application::db()->table('')->where('')->select();
Application::db()->table('zq_user')->where('id=11')->delete();
//占位操作
Application::db()->select('select * from test_table where id = ?',[1]);

自动加载使用说明


//类加载使用方法

//configure目录下存在map.php配置文件

return [
    //psr4命名空间注册
    'autoload_namespace_psr4'   =>  [
        'app\\'         =>  [
            ROOT_PATH . 'application'
        ],
        'bootstrap\\'         =>  [
            ROOT_PATH . 'bootstrap/bootstrap'
        ],
        'linkphp\\'               =>  [
            FRAMEWORK_PATH . 'linkphp'
        ],
    ],
    //psr0命名空间
    'autoload_namespace_psr0' => [
        //'命名空间' => '映射路径地址'
    ],
    //指定自动加载机制排序
    'autoload_namespace_file' => [
        //'文件名' => '映射路径地址'
        'app_func'                => LOAD_PATH . 'common.php',
        'framework_func'          => FRAMEWORK_PATH . 'helper.php'
    ],
    'class_autoload_map' => [
        //'类名' => '类文件地址'
    ],
];

//建议使用psr4标准注册自己的相关命名空间

//如需指定加载相关文件将其放入 autoload_namespace_file下,指定到具体的文件名,框架启动便会扫描加载进框架内

restful使用说明

use framework\Application;

//使用linkphp开发api应用

//目前框架支持3种格式输出,默认输出方式为json,其余为xml、以及view

//需要输出相关格式内容只需要在configure.php配置文件中进行配置

//然后在控制器中方法下使用

return ['test' => 'test'];

//将数组进行返回,框架的response类会根据配置的信息进行相应的格式转换进行返回给请求者

//判断当前请求方式可使用框架封装的

Application::httpRequest()->isGet();

request()->isGet();

//支持一下方式判断

request()->isGet();
request()->isPost();
request()->isDelete();
request()->isPut();
request()->isHead();
request()->isOptions();
request()->isPatch();

容器使用说明


//依赖注入实现

//controller 层构造方法注入

namespace app\controller\main;

use linkphp\http\HttpRequest;
use framework\Application;

class Home
{
    public function __construct(HttpRequest $httpRequest)
    {
        dump($httpRequest);
    }
}

//通过浏览器请求到该控制器会将HttpRequest自动注入进Home控制器中

//自行手动注入  闭包注入实现
Application::singleton(
    'envmodel',
    function(){
        //自行操作最后返回一个实例对象
        return Application::get('test');
    });

//自行手动注入  类名注入
Application::singletonEager(
    'run',
    'linkphp\console\Command'
);

//获取类实例
Application::get('test');

//通过类名获取时,如果在容器内未找到实例会执行自动实例操作并返回,自动实例也会触发自动注入,如果实例对象存在依赖类会自动进行注入
Application::get('linkphp\console\Command');


Application::bind(
    Application::definition()
        ->setAlias('test')
        ->setIsEager(true)
        ->setIsSingleton(true)
        ->setClassName('classname')
);

Application::bind(
    Application::definition()
        ->setAlias('test')
        ->setIsEager(true)
        ->setIsSingleton(true)
        ->setCallBack('callback')
);

缓存使用说明


use framework\Application;

//获取缓存
cache('test');
Application::cache('test');

//写入缓存
cache('test','test');
Application::cache('test','test');

参数接收使用方法



use framework\Application;

//input参数接收操作

Application::input();

Application::input('get.');

Application::input('get.test');

Application::input('get.in',function($value){
            //闭包实现
            //这里写具体的过滤方法
            //自定义
            //记得返回处理好的
            return $value;
        });

//第一个参数为接收一个get请求参数值,第二个参数为过滤获取值,可接受一个闭包函数,自定义闭包方法,闭包函数接收一个原始get值,在其里面进行自定义过滤
//最后必须将值进行返回,执行框架默认的过滤函数

//助手函数使用

input();

input('test');

input('get.');

input('get.test',function($value){
    //闭包实现
    //这里写具体的过滤方法
    //自定义
    //记得返回处理好的
    return $value;
});

request()->input('get.');

request()->input('get.',function($value){
    //闭包实现
    //这里写具体的过滤方法
    //自定义
    //记得返回处理好的
    return $value;
});

中间件使用说明


use framework\Application;
//中间件使用

//框架中实现了6个中间件

//configure目录下middleware.php中间件配置文件中

return [

    'beginMiddleware'          => [
        \app\controller\main\Index::class,
        \app\controller\main\Test::class,
        \app\controller\main\First::class,
    ],

    'appMiddleware'            => [
        \app\controller\main\Index::class,
    ],

    'modelMiddleware'          => [
        \app\controller\main\Test::class,
    ],

    'controllerMiddleware'     => [
        \app\controller\main\Test::class,
    ],

    'actionMiddleware'         => [
        \app\controller\main\Test::class,
    ],

    'destructMiddleware'       => [
        \app\controller\main\Test::class,
    ],

];

//分别为 框架启动中间件、应用启动中间件、模块初始化中间件、控制器初始化中间件、方法调用中间件以及框架销毁前中间件

//使用中间件的类必须实现handle方法,接收一个闭包参数最后都必须把闭包赋值的变量做返回操作

use Closure;

class Index
{
    public function handle(Closure $next)
    {
        dump('middleware index');
        return $next;
    }
}

//闭包使用方法

Application::middleware('beginMiddleware',function (Closure $v) {
    $v();
    echo 3;
    return $v;
});

路由使用说明


use framework\Application;
//路由使用

//在src目录下route.php路由配置文件中进行配置使用

//Router::get(':id/:test', '/addons/test/Test@main');
Router::get('/', '/http/home/main');

return [
//    ':id/:test'   =>  ['/main/home/main',['method' => 'get']],
];

//键名为当前请求的路径
//键值接收字符串或者闭包函数


//闭包使用方法

use linkphp\http\HttpRequest;

Router::get(':id/:test', function(HttpRequest $httpRequest,$id){
    dump($id);
    dump($httpRequest);
    return '闭包路由';
});

视图使用说明


use framework\Application;
//view层使用

Application::view('main/home/main',[
    'linkphp' => 'linkphp'
]);

//助手函数使用方法

//加载页面
view('main/home/main');

//加载页面并传值
view('main/home/main',['linkphp' => 'linkphp']);

验证器使用


Validator::data('w')
            ->withValidator('url', function ($validator, $input){
                $validator->addValidator($input,['rule' => [
         'class' => 'url', 'param' => []
        ], 'errorMessage' => '非法URL地址'
       ]);
            });
        if(!Validator::check()){
            dump(Validator::geterror());die;
        }

分页器使用


use linkphp\page\Paginator;

//查出待分页的总数
$count = Db::table('lp_user')->count('id');
//实例化分页器传入总数以及每页数量
$page = new Paginator($count,2);
//获取limit参数
$data = Db::table('lp_user')->limit($page->limit())->select();
//渲染分页条
$page->render();

命令行使用说明


首先请保证已经将执行目录切换到linkphp框架目录默认执行
php cli如果已经将cli文件更换为自定义名称则执行自定义的名称

php cli +命令名

系统内置了些许方法可参考手册查找如果需要自定义命令名则需要做一下几步

1在configure目录中打开command.php文件添加命令类
2在新增的命令类中按照系统给定的方式进行创建类文件
假如自定义命令类为 app\command\main\Tset

namespace app\command\main;

use framework\Application;
use linkphp\console\Command;

class Test extends Command
{

    public function configure()
    {
        $this->setAlias('test')->setDescription('test');
    }

    public function execute()
    {
        Application::get('linkphp\console\command\Output')->writeln('this is test');
    }
    
    //如果需要在该命令中添加更多方法则需要集成该方法
    public function commandHandle(){}
}
Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "{}" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright {yyyy} {name of copyright owner} Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

简介

基于swoole一款高性能多进程常驻内存型全栈框架,内置WebSocket服务器、服务治理PhpRpc功能,不依赖传统的 PHP-FPM,可以用于构建高性能的Web系统、API、中间件、基础服务等等。QQ:750688237 展开 收起
PHP
Apache-2.0
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
PHP
1
https://gitee.com/ydg/linkphp.git
git@gitee.com:ydg/linkphp.git
ydg
linkphp
linkphp
master

搜索帮助

344bd9b3 5694891 D2dac590 5694891