Download the PHP package php-openapi/yii2-openapi without Composer

On this page you can find all versions of the php package php-openapi/yii2-openapi. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package yii2-openapi

yii2-openapi

REST API application generator for Yii2, openapi 3.0 YAML -> Yii2.

Base on Gii, the Yii Framework Code Generator.

Latest Stable Version Latest Alpha Version Total Downloads License yii2-openapi

TLDR; what is this?

A code generator for OpenAPI and Yii Framework based PHP API application.

Input: OpenAPI 3.0 YAML or JSON (via cebe/php-openapi)

Output: Yii Framework Application with Controllers, Models, database schema

Features

Currently available features:

Requirements

Install

composer require php-openapi/yii2-openapi:^2.0@beta

Usage

You can use this package in your existing application or start a new project using the yii2-app-api application template. For usage of the template, see instructions in the template repo readme.

In your existing Yii application config (works for console as well as web):

To use the web generator, open index.php?r=gii and select the REST API Generator.

On console, you can run the generator with ./yii gii/api --openApiPath=@app/openapi.yaml. Where @app/openapi.yaml should be the absolute path to your OpenAPI spec file. This can be JSON as well as YAML (see also php-openapi/php-openapi for supported formats).

Run ./yii gii/api --help for all options. Example: Disable generation of migrations files ./yii gii/api --generateMigrations=0

See Petstore example for example OpenAPI spec.

OpenAPI extensions

This library understands the following extensions to the OpenAPI spec:

x-faker

You may specify custom PHP code for generating fake data for a property:

To avoid generating faker code for particular model attribute, use value false:

Using in reference with allOf:

x-table

Specify the table name for a Schema that defines a model which is stored in the database. You can generate non-db model based on \yii\base\Model without migrations by setting x-table: false

x-pk

Explicitly specify primary key name for table, if it is different from "id"

x-db-type

Explicitly specify the database type for a column. (MUST contain only real DB type! (json, jsonb, uuid, varchar etc.)). If x-db-type is set to false, property will be processed as virtual; It will be added in model as public property, but skipped for migrations generation.

Example values of x-db-type are:

Such values are not allowed:

If enum and x-db-type both are provided then for database column schema (migrations), only x-db-type will be considered ignoring enum.

x-indexes

Specify table indexes

If raw DB expression is needed in index, then it must be for only one column. Example:

x-db-default-expression

Ability to provide default value by database expression

Note: If both default and x-db-default-expression are present then default will be considered.

Also see: https://dev.mysql.com/doc/refman/8.0/en/data-type-defaults.html

x-fk-on-delete

Allow to set foreign key constraint in migrations for ON DELETE event of row in database table. Example:

x-fk-on-update

Allow to set foreign key constraint in migrations for ON UPDATE event of row in database table. For example, see above section for x-fk-on-delete.

x-fk-column-name

Provide custom database table column name in case of relationship column. This will not reflect in models relations, faker etc. Example:

x-deleted-schemas

This is root level key used to generate "drop table" migration for the deleted component schema. If a component schema (DB model) is removed from OpenAPI spec then its following entities should be also deleted from the code:

So to generate appropriate migration for the removed schema, explicitly setting schema name or schema name + custom table name is required in this key. Only then the migrations will be generated. It should be set as:

x-no-relation

To differentiate a component schema property from one-to-many or many-to-many relation in favour of array(json) of related objects, x-no-relation (type: boolean, default: false) is used.

This will not generate 'comments' column in database migrations. But it will generate getComments() relation in Yii model file.

In order to make it real database column, extension x-no-relation can be used.

Database column type can be array, json etc. to store such data.

Now if the Comment schema from the above example is

then the value for comments can be

x-no-relation can be only used with OpenAPI schema data type array.

x-route

To customize route (controller ID/action ID) for a path, use custom key x-route with value <controller ID>/<action ID>. It can be used for non-crud paths. It must be used under HTTP method key but not directly under the paths key of OpenAPI spec. Example:

It won't generate actionCreateInvoice in PaymentsController.php file, but will generate actionInvoice instead in same file.

Generated URL rules config for above is (in urls.rest.php or pertinent file):

Also, if same action is needed for HTTP GET and POST then use same value for x-route. Example:

Generated URL rules config for above is (in urls.rest.php or pertinent file):

x-route does not support Yii Modules.

x-description-is-comment

boolean; default: false

When a new database table is created from new OpenAPI component schema, description of a property will be used as comment of column (of database table).

This extension is used when a description is edited for existing property, and you want to generate migration for its corresponding column comment changes.

This extension can be used at 3 place:

1. root level (highest priority)

This will create migration of any changed description of component schema property present throughout the spec.

2. component schema level

This will create migration of changed description of only properties of component schema which have this extension.

3. property level (lowest priority)

Migrations will be only generated for changed description of properties having this extension.

Many-to-Many relation definition

There are two ways for define many-to-many relations:

Simple many-to-many without junction model

Many-to-many with junction model

This way allowed creating multiple many-to-many relations between to models

Handling of NOT NULL constraints

NOT NULL in DB migrations is determined by nullable and required properties of the OpenAPI schema. e.g. attribute = 'my_property'.

Handling of enum (#enum)

It works on all 3 DB: MySQL, MariaDb and PgSQL.

Note: Changes in enum values are not very simple. For Mysql and Mariadb, migrations will be generated but in many cases custom modification in it are required. For Pgsql migrations for change in enum values will not be generated. It should be handled manually.

It will be ignored for database column schema (migrations) if x-db-type is provided.

Handling of numeric (#numeric, #MariaDb)

precision-default = 10 scale-default = 2

DB-Result = decimal(12,4)

DB-Result = decimal(12,2)

DB-Result = decimal(10,2)

Handling of timestamp database column data type

If field is defined as

then database type selected will be timestamp. This is by design. If datetime data type is needed, use x-db-type as

Assumptions

When generating code from an OpenAPI description there are many possible ways to achive a fitting result. Thus, there are some assumptions and limitations that are currently applied to make this work. Here is a (possibly incomplete) list:

Other things to keep in mind:

Adding columns to existing tables

When adding new fields in the API models, new migrations will be generated to add these fields to the table. For a project that is already in production, it should be considered to adjust the generated migration to add default values for existing data records.

One case where this is important is the addition of a new column with NOT NULL contraint, which does not provide a default value. Such a migration will fail when the table is not empty:

Fails on a PostgreSQL database with

add column name string(128) NOT NULL to table {{%company}} ...Exception: SQLSTATE[23502]: Not null violation: 7 ERROR: column "name" contains null values

The solution would be to create the column, allowing NULL, set the value to a default and add the null constraint later.

Screenshots

Gii Generator Form:

Generated files:

Development

To contribute or play around, steps to set up this project up locally are in CONTRIBUTING.md.

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".


All versions of yii2-openapi with dependencies

PHP Build Version
Package Version
Requires php Version >=7.1.0
cebe/php-openapi Version ^1.5.0
yiisoft/yii2 Version ~2.0.15
yiisoft/yii2-gii Version ~2.0.0 | ~2.1.0 | ~2.2.0| ~2.3.0
laminas/laminas-code Version ^3.4
insolita/yii2-fractal Version ^1.0.0
fakerphp/faker Version ^1.9
sam-it/yii2-mariadb Version ^2.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package php-openapi/yii2-openapi contains the following files

Loading the files please wait ....