sofical/restphp-core

php RESTFul api router framework

v3.1.0 2024-11-21 14:31 UTC

This package is auto-updated.

Last update: 2025-06-07 03:24:10 UTC


README

介绍

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