定制 vikkio88/nicu 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

vikkio88/nicu

Composer 安装命令:

composer create-project vikkio88/nicu

包简介

README 文档

README

developing micro rest api in seconds, shared hosting friendly This is a simple startup project for a nicu restful api.

Create a project

composer create-project vikkio88/nicu NAME_OF_PROJECT

Run locally

to run this app locally using the php server just type

php start.php

This will use the router.php to simulate a simple URLRewrite

To change the default port edit the config/app.json

{
  "app": {
    "version": 0.1,
    "port": 8088 <--
  }
}

Or pass it as a parameter to the start.php script

php start.php 8090

Run via docker

you can edit the file docker-compose.yml in order to change the name of the image.

docker-compose up -d

The app will be reachable at the port you specified in the docker-compose.yml file.

How To

Add a new route

Just go to config/routes.php and add a new route following the syntax:

[
    'route' => '/NAME_OF_THE_ROUTE',
    'method' => 'get',
    'action' => AppSampleAction::class
]

There are three parameter to specify per each route:

  1. Route The syntax for the routes is the one specified in here.
  2. Method This is the HTTP verb and can be any HTTP request type (options,post,get).
  3. Action this is the Action that will be invoked when the route is matched, as long as it extends ApiAction and implements action() method returning an array it will work fine.

Add new / read Config from action

Config lib is provided by hassankhan/config, here you can find some example of config files that are supported.

Set

To add a config, add a file or a simple array key to the one specified:

'someStuff' => 'Stuff'

to get it

// $config being an instance of Config
$config->get('someStuff', 'fallback value');

Get

if Config will be injected in the constructor, you will have access to all the configs in the 'config/ folder'

class MyAction extends ApiAction {

    public function __construct(Config $config)
    {
        $this->config = $config;
    }

    protected function action(): array
    {
        return [
            'test' => $this->config->get('stuff')
        ];
    }
}

Setup the Di Container

This small framework integrated PhpDi instead of Pimple, as autowiring makes everything a bit easier to read.

To link the interface to the implementation, you need to specify a provider into config/providers.

The provider body, will work a bit like the Laravel Providers, you will need to specify, using php-di syntax the bind between interface and implementation.

The php-di injection will autowire whatever is configured in those providers, and will inject the implementation on whatever you are creating injecting it into the Action constructor.

Example

config/providers.php

return [
    'providers' => [
        SampleProvider::class
    ]
];

SampleProvider.php

class SampleProvider extends Provider
{

    public function boot()
    {
        $this->bind(Interface::class, function(ContainerInterface $c){
            return new Implementation($c->get(AnotherInterface::class));
        });
    }
}

Middlewares

Slim (so PSR) Middlewares are supported by nicu. To find out more read this.

To add a new one you need to specify it into the file config/app.php under the config key app.middlewares:

'middlewares' => [
    Cors::class => [
        "origin" => ["*"],
        "methods" => ["GET", "POST", "PUT", "PATCH", "DELETE"],
    ],
    function(RequestInterface $request, ResponseInterface $response,closure $next){
        // do something
        $next($request,$response);
        return $response;
    }
]

As ↑ shows, the config can be provided in two ways, either as a Middleware class, specifying a config as value in the array (config being an array too), or as a simple \Callable.

DotEnv

You can specify a config as environment variable in a .env file.

SOME_STUFF="I like trains"

those will be loaded on the index.php on the bootstrap phase.

And you can inject it into a config like this.

'stuff' => getenv('SOME_STUFF')

For more info about this check out vlucas/phpdotenv docs.

Build

This framework allows you to create a build version of your rest api, and it will cleanup the vendor folder in order to have less file to upload.

To run the build just type

composer run build

This will create a cleaned up/production ready version of the app in the subfolder ./dist/ ready to be uploaded to your Apache or Nginx server.

统计信息

  • 总下载量: 12
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-05-08

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固