deweppro/framework

此包已被废弃,不再维护。未建议替代包。

RESTful API 框架

v3.3.0 2020-02-02 23:17 UTC

This package is auto-updated.

Last update: 2023-06-29 01:28:33 UTC


README

skeleton: https://github.com/deweppro/framework-api-skeleton

如何初始化实例

<?php declare(strict_types=1);

require_once './../vendor/autoload.php';

Dewep\Config::setConfigPath(__DIR__.'/../config.yml');

(new Dewep\Application())->bootstrap();

一个示例配置文件 [config.yml]

显示发生错误的响应文件

debug: true

标量答案的答案格式为 text/html; charset=utf-8,对于非标量答案:

response: application/json

路由

您可以通过配置文件添加路由

routes:
  /{user}/name:
    GET,POST,PUT: Dewep\Demo::demo
  /:
    GET: Dewep\Demo::home

其中占位符 {user} 将作为变量传递给控制器

public function demo(Request $request, string $user) {}

或者您可以指定路由生成器,它必须包含一个方法 - public function handler()

routes: \App\Routes
class Routes
{
    public function handler()
    {
        $routes = [];

        $routes['/{user}/name']['GET'] = 'Dewep\Demo::demo';
        $routes['/{user}/name']['POST'] = 'Dewep\Demo::demo';
        $routes['/{user}/name']['PUT'] = 'Dewep\Demo::demo';
        
        $routes['/']['GET'] = 'Dewep\Demo::home';

        return $routes;
    }
}

中间件

您可以在主代码执行前后指定要执行的中间件。中间件应匹配接口 \Dewep\Interfaces\MiddlewareInterface

middleware:
    before:
        cookie: &CookieUserAuth
            _: \Dewep\Middleware\Auth\Cookies
            name: sess
            secret: 'demo'
            exp: 6000000
            domain: localhost

    after:
        cookie: *CookieUserAuth

依赖注入

对于依赖注入,您可以创建必须匹配接口 Dewep\Interfaces\ProviderInterface 的提供者

providers:
  logger:
    _: Dewep\Providers\LoggerProvider
    debug: true
    name: app
    filename: app.log

  mysql:
    _: Dewep\Providers\MysqlProvider
    host: localhost
    port: 3306
    dbname: default
    login: default
    password: default

通过 Container 访问提供者: Container::get('logger')->...

控制台

通过控制台应用程序与控制台一起工作

#!/usr/bin/env php
<?php

include_once __DIR__.'/vendor/autoload.php';

\Dewep\Config::setConfigPath(__DIR__.'/config.yml');

(new Dewep\Console())->bootstrap();

控制台命令如下添加到配置文件: 命令名称:类

console:
    app.restore: \Dewep\Handlers\Consoles\CreateDirs

所有命令类都必须匹配接口 \Dewep\Interfaces\ConsoleInterface

执行带有参数的命令

./console app.restore --demo=hello

查看命令列表

./console
Commands list:  
        app.restore: Restore the system directory structure.
        ....

目录结构

root |
     | app          [source code for the application]
     | public       [public folder]
     | database     [database migration]]
     | resources    [templates template engine and data used in the application]
     | storage      [the preservation of data generated by users and application]
     | temp         [folder logs and temporary files]
     | tests        [folder for unit tests]