• Stars
    star
    104
  • Rank 330,604 (Top 7 %)
  • Language
    PHP
  • License
    MIT License
  • Created over 5 years ago
  • Updated 10 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

OpenAPI Spec to API in 3, 2, 1... done!

yii2-app-api

OpenAPI Spec to API in 3, 2, 1... done!

Yii Framework Application Template for quickly building API-first applications.

Based on yii2-openapi (code generator) and php-openapi (specification reader and validator).

Latest Stable Version Total Downloads License

Demo

asciicast

Overview

This application template does not contain any useful code, it only provides the directory structure for your API project. All the code is generated from an OpenAPI API description file.

When working with this template you follow the API Design-First Approach. So to get started building an API with this application template you need an API description first. If you don't have one yet, you might want to check the following resources first:

If you have an OpenAPI description, you can continue with the next steps:

  1. Setup
  2. Generate Code

More Docs and Guides can be found in /docs.

Setup

There are two different ways:

  • PHP directly on your machine
  • Using Docker

PHP directly

Having PHP and a database server installed on your machine, you can run the following commands:

composer create-project cebe/yii2-app-api my-api
cd my-api
cp env.php.dist env.php
cp config/components-ENV.local.php config/components-dev.local.php

You need to adjust config/components-dev.local.php to configure your database connection.

After that, run make start and make stop to control the PHP webservers for API and backend.

You may use XDEBUGW=1 to enable xdebug for the webserver, e.g. make XDEBUGW=1 start.

You can now continue with generating code.

Using Docker

You need Docker and Docker Compose installed.

Create the repository:

composer create-project cebe/yii2-app-api my-api

For the easiest way you need GNU make, then run:

make start-docker

This uses docker-compose to start docker containers for the API and the backend including a database.

You can now continue with generating code.

Note: If you don't have GNU make, you need to copy the configuration files as in the PHP directly section, then run:

cp docker-compose.override.dist.yml docker-compose.override.yml
docker-compose up -d
docker-compose exec backend-php sh -c 'cd /app && composer install'

Generate Code

At this point you can start generating your API code to play with it by following the instructions below. If you'd like to start a real project instead of just playing around you can find more detailed documentation in docs/README.md specifically the "Getting Started"-Section.

Console

Tip: If you use Docker, run make cli before running any of these commands.

Run ./yii gii/api to generate your API code. The --openApiPath parameter specifies the path to your OpenAPI spec file. The following example will generate API code for the OpenAPI petstore example.

./yii gii/api --openApiPath=https://raw.githubusercontent.com/OAI/OpenAPI-Specification/3.0.2/examples/v3.0/petstore-expanded.yaml

Note: If the OpenAPI spec file is present locally on the file system, then openApiPath must be specified as an absolute path, relative paths will not work correctly. On UNIX based OS it must start with /. Example: /home/user/documents/MyProjectOpenAPISpec.yml

Run ./yii gii/api --help for a list of configuration options. You may also adjust the configuration in config/gii-generators.php.

Then set up the database:

./yii migrate/up
./yii faker

Web

To use the web generator, open http://localhost:8338/gii and select the REST API Generator.

Gii - REST API Generator

Enter the path or URL to the "OpenAPI 3 Spec file", e.g. https://raw.githubusercontent.com/OAI/OpenAPI-Specification/3.0.2/examples/v3.0/petstore-expanded.yaml.

Click "Preview":

Gii - REST API Generator - Generated files

Click "Generate" to generate API files.

Then set up the database by running the following commands on the command line:

./yii migrate/up
./yii faker

Try it

cd api
make start

Your API is now available at http://localhost:8337/. Try to access an endpoint of your spec via curl:

$ curl http://localhost:8337/pets
[
    {
        "name": "Eos rerum modi et quaerat voluptatibus.",
        "tag": "Totam in commodi in est nisi nihil aut et."
    },
    {
        "name": "Voluptas quia eos nisi deleniti itaque aspernatur aspernatur.",
        "tag": "Temporibus id culpa dolorem sequi aut."
    },
    {
        "name": "Facere aut similique laboriosam omnis perferendis et.",
        "tag": "Quo harum quo et ea distinctio non quam."
    },
    ...
]

Application structure

This application template consists of 3 application tiers:

  • api, contains the Yii application for the REST API.
  • console, contains the Yii application for console commands, cronjobs or queues (yii command).
  • backend, contains the Yii application for a CRUD backend on the API data.

The following list explains the directory structure in more detail:

  • api/ - API application tier

    • config/ - configuration for API tier
      • url-rules.php - custom URL rules
      • url-rules.rest.php - URL rules generated from OpenAPI Description
      • components.php - application components
      • app.php - Yii application config (+ overrides for different environments app-*.php)
    • controllers/ - Controller classes generated from OpenAPI Description
    • web/ - public web directory for API application
  • backend/ - Backend application tier

    • config/ - configuration for Backend tier
      • components.php - application components
      • app.php - Yii application config (+ overrides for different environments app-*.php)
    • controllers/ - Controller classes
    • views/ - View files
    • web/ - public web directory for Backend application
  • common/ - common code files

    • models/ - model classes generated from OpenAPI Description
    • migrations/ - database migrations generated from OpenAPI Description
  • config/ - Common configuration for all application tiers

    • components.php - Yii application components (+ overrides for different environments components-*.php)
    • env.php - Environment setup (YII_DEBUG, YII_ENV, path aliases, composer autoloader)
    • events.php - Class wide event listeners
    • gii-generators.php - configuration for the Gii code generator (allows to set default values for the ApiGenerator)
    • params.php - Configuration for Yii::$app->params
  • console/ - Console application tier

    • config/ - configuration for Console tier
      • components.php - application components
      • app.php - Yii application config (+ overrides for different environments app-*.php)
  • logs/ - log files

  • runtime/ - temporary runtime files

Development

Below commands are helpful while developing this project:

./yii gii/api --openApiPath=/app/openapi/schema.yaml --generateMigrations=0  --generateControllers=0 --generateUrls=0

./yii gii/api --openApiPath=/app/openapi/schema.yaml --generateMigrations=1  --generateControllers=0 --generateUrls=0 --generateModels=0 --generateModelFaker=0

Support

Need help with your API project?

Professional support, consulting as well as software development services are available:

https://www.cebe.cc/en/contact

Development of this library is sponsored by cebe.:cloud: "Your Professional Deployment Platform".

More Repositories

1

markdown

A super fast, highly extensible markdown parser for PHP
HTML
994
star
2

php-openapi

Read and write OpenAPI yaml/json files and make the content accessible in PHP objects.
PHP
467
star
3

pdfpc-latex-notes

Latex Package that allows creating a pdfpc compatible notes file directly from your latex presentation \notes.
TeX
144
star
4

js-search

A client side search engine for use on static pages.
PHP
139
star
5

yii2-openapi

REST API application generator for Yii2, openapi 3.0 YAML -> Yii2
PHP
131
star
6

assetfree-yii2

A composer package that allows you to install yii2 without composer-asset-plugin
PHP
50
star
7

yii2-lifecycle-behavior

Define the lifecycle of a model by defining allowed status changes.
PHP
50
star
8

markdown-latex

A markdown parser for converting markdown to LaTeX written in PHP.
PHP
44
star
9

yii2-gravatar

Gravatar Widget for Yii Framework 2
PHP
40
star
10

git-simple-subsplit

A git subtree/subsplit script for quickly creating one-way subsplit of repositories. (use for composer packages)
Shell
20
star
11

indent

A small tool to convert text file indentation
PHP
17
star
12

trace-graph

tool to create a network graph from different traceroutes
PHP
13
star
13

luya-module-sitemap

πŸ“‡ sitemap.xml module for LUYA CMS
PHP
12
star
14

humhub-deployment

Reliable deployments for Humhub. Install Humhub with modules, themes and custom configuration.
Makefile
10
star
15

color-nick

A simple PHP lib that can color nick names to make them distinguishable in a chat room.
PHP
8
star
16

yii2-vuejs

Vue JS asset for Yii 2
PHP
6
star
17

yii2-psr7-messages

PSR7 HTTP Request and Response classes for yii2
PHP
6
star
18

yii2-oauth-server-example

Example application to demonstrate the implementation of an OAuth-Server with Yii 2
PHP
6
star
19

pulse-php-discover

A PHP implementation of the pulse/syncthing cluster discovery protocol.
PHP
5
star
20

gnucash-php

A library for reading gnucash XML format in PHP
PHP
5
star
21

yii-course-example-code

Example code of my yii 1.1 training course
PHP
5
star
22

yii2-oauth-client-example

Example application to demonstrate the implementation of an OAuth-Client with Yii 2
4
star
23

yii2-loki-log-target

Grafana Loki Log Target for Yii2
PHP
4
star
24

jsonstore

A dead simple json file storage.
PHP
3
star
25

make-php

A simple script to build different PHP versions
Shell
3
star
26

clean-docblox-theme

A Docblox template that offers a clean tidy frontend to your docs.
3
star
27

commonmark-latex

A LaTeX extension for https://github.com/thephpleague/commonmark
3
star
28

yii-base-application

A more sophisticated yii base application to start new projects from
PHP
2
star
29

code-dashboard

code dashboard shows your git commits and lets you comment on code and changes
PHP
2
star
30

yii2-debug-profiler

xhprof/uprofiler based profiling added to yii2 debug toolbar
2
star
31

tex-presentation

Template for starting a new latex beamer presentation.
TeX
1
star
32

yii-app-testrunner-standalone

standalone version of yii app testrunner
PHP
1
star
33

bower-asset

This package is registered on packagist to reserve the namespace for the composer-asset-plugin.
1
star
34

commonmark-smartypants

A smartypants extension for https://github.com/thephpleague/commonmark
1
star
35

yiimap.com

A map to show Yii users in the world and bring them together for meetups.
PHP
1
star
36

npm-asset

This package is registered on packagist to reserve the namespace for the composer-asset-plugin.
1
star
37

smartyml

A Multi-Language plugin for smarty template engine
1
star
38

test-gitrepostub

test repo
1
star
39

yii2-activerecord-benchmark

A benchmark to test yii active record performance for different backends
PHP
1
star
40

github-tracker

advanced github notification and pull request viewer
PHP
1
star
41

website-down

the code used while migrating yiiframework.com to a new server.
PHP
1
star
42

yii-app-testrunner

phpunit unittest runner for yii applications
PHP
1
star
43

yiidoc-test

testing something for yiidoc
1
star
44

demo.cebe.cc

Demo site for my yii extensions and some other stuff that comes out while developing cool applications.
PHP
1
star
45

yii2-statsd

A statsd component for Yii 2 to collect profiling and other metrics
1
star
46

db-state-driver

A tool that saves db state for testing purpose.
PHP
1
star