1 Star 2 Fork 0

sofical/restphp-core

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

restphp-core

介绍

RestPHP框架核心代码

软件架构

轻便的插件化框架,可丰富你的项目的路由器选择

安装教程

  1. 使用composer引入sofical/restphp-core到您的项目,参考命令:composer require sofical/restphp-core:dev-master

  2. 项目脚手架搭建(例:https://gitee.com/sofical/restphp ):

2.1. 构建入口文件,如:bootstrap/index.php

define('PROJECT_ROOT', '../');

// 自动加载composer包
require(PROJECT_ROOT . 'vendor/autoload.php');

//引入RESTPHP配置
require(PROJECT_ROOT . 'config/rest.config.php');

//引入项目配置
require(PROJECT_ROOT . 'config/proj.config.php');

//引入框架
require(DIR_RESTPHP . '/Rest.php');

\restphp\Rest::run();

2.2. 将运行容器根目录配置到程序入口文件所在目录,如:bootstrap

配置URL重新规则,将所有请求地址重写到第一步的程序入口文件。如,Nginx重写配置:

location / {
    index  index.php;
    if (!-e $request_filename) {            
        rewrite ^/(.*)$ /index.php?$1 last;                
    }            
}

2.3. 项目配置文件,必要内容为(目录建议为:config):

RESTPHP环镜配置文件,名称可自定义,如:rest.config.php

//RESTPHP 相关配置
define('REST_PHP_VERSION', '3.0');
define('DIR_LIB', PROJECT_ROOT . 'lib');
define('DIR_RESTPHP', PROJECT_ROOT . 'vendor/sofical/restphp-core/src');
define('DIR_BUILD', PROJECT_ROOT . 'src/php');
define('DIR_BUILD_TARGET', PROJECT_ROOT . 'runtime/target');
define('HTTP_VERSION', '1.1');
define('CONTENT_TYPE', 'application/json');
define('SYS_TIME', time());
define('SYS_MICRO_TIME', microtime(true));

项目配置文件,文件名称可自定义,如:proj.config.php

//当前环镜
define('PROJ_ENV', 'dev');

//环镜参数
$_EVN_PARAM_ALL = include('env.config.php');
$_EVN_PARAM = $_EVN_PARAM_ALL[PROJ_ENV];
$_DB_MYSQL = isset($_EVN_PARAM) ? $_EVN_PARAM['DATABASE_MYSQL'] : array();

//加载多语言
$_LANG = include('lang.config.php');

//日志目录
define('APP_LOG_DIR', PROJECT_ROOT . 'runtime/log');

多语言配置文件,文件名称和上一步$_LANG定义一致,如:lang.config.php

<?php
return array(
    'zh-cn' => array(
        '[REST_DB_UNKNOWN_EXCEPTION]' => '数据库未知异常',
        '[REST_DB_NOT_SUPPORT_DB_LINK]' => '不支持的数据库链接方式',
        '[REST_DB_HOST_REFUSED]' => '数据库链接失败',
        '[REST_DB_CONFIG_NOT_EXISTS]' => '数据库配置不存在'
    )
);

项目环镜差异化配置文件。配置文件名称和2.3的$_EVN_PARAM定义一致,如:env.config.php

return array(
    'dev' => array(
        'DATABASE_MYSQL' => array(
            'DB_PREFIX' => '', //表前缀
            'Def_Select'	=> array(	//查询库,一般是从库或只读库
                'dbhost'	=> '127.0.0.1',
                'dbport'	=> 3306,
                'dbuser'	=> 'root',
                'dbpass'	=> '123456',
                'dbname'	=> 'restphp-example',
                'charset'	=> 'UTF8',
                'dbtype'	=> 1,
                'linktype'	=> 1,
                'fetchtype'	=> 1
            ),
            'Def_Update'	=> array(	//有数据修改权限的库,一般指主库
                'dbhost'	=> '127.0.0.1',
                'dbport'	=> 3306,
                'dbuser'	=> 'root',
                'dbpass'	=> '123456',
                'dbname'	=> 'restphp-example',
                'charset'	=> 'UTF8',
                'dbtype'	=> 1,
                'linktype'	=> 1,
                'fetchtype'	=> 1
            )
        )
    ),
    'pro' => array(
        'DATABASE_MYSQL' => array(
            'DB_PREFIX' => '', //表前缀
            'Def_Select'	=> array(	//查询库,一般是从库或只读库
                'dbhost'	=> '127.0.0.1',
                'dbport'	=> 3306,
                'dbuser'	=> 'root',
                'dbpass'	=> '123456',
                'dbname'	=> 'restphp-example',
                'charset'	=> 'UTF8',
                'dbtype'	=> 1,
                'linktype'	=> 1,
                'fetchtype'	=> 1
            ),
            'Def_Update'	=> array(	//有数据修改权限的库,一般指主库
                'dbhost'	=> '127.0.0.1',
                'dbport'	=> 3306,
                'dbuser'	=> 'root',
                'dbpass'	=> '123456',
                'dbname'	=> 'restphp-example',
                'charset'	=> 'UTF8',
                'dbtype'	=> 1,
                'linktype'	=> 1,
                'fetchtype'	=> 1
            )
        )
    )
);

2.4. 创建测试路由文件,src/php/example/ControllerIndex.php

<?php
namespace example;

use restphp\http\RestHttpResponse;/**
 * @RequestMapping("")
 */
class ControllerIndex {
    /**
     * @RequestMapping("/hello", method="GET")
     * @return void
     */
    public function index() {
        RestHttpResponse::html("hello world!");
    }
}

2.5. 构建路由,项目根目录下创建一个文件,如:build.php

<?php
/**
 * 构建入口
 * @author sofical
 * @date 2017-03-17
 */

define('PROJECT_ROOT', '');

// 自动加载composer包
require(PROJECT_ROOT . 'vendor/autoload.php');

//引入目录配置
require('config/rest.config.php');

//引入框架
require(DIR_RESTPHP . '/Rest.php');

\restphp\Rest::build();

运行命令:php build.php 构建路由映射关系

2.6. 访问项目地址,如:http://127.0.0.1/hello 。输出:hello world!

更多使用说明,请参考:https://www.restphp.com

MIT License Copyright (c) 2024 sofical Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

RestPHP框架核心代码 展开 收起
README
MIT
取消

发行版 (1)

全部
8个月前

贡献者

全部

近期动态

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

搜索帮助