Skip to content

Commit fd0ae1f

Browse files
committed
environment structure and config
1 parent 89cacbc commit fd0ae1f

28 files changed

+413
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/vendor

README.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,20 @@
11
# yii2-app-api
2-
OpenAPI Spec to API in 3, 2, 1... done!
2+
3+
> OpenAPI Spec to API in 3, 2, 1... done!
4+
5+
Yii Framework Application Template for quickly building API-first applications.
6+
7+
## Setup
8+
9+
git clone https://github.com/cebe/yii2-app-api my-api
10+
cd my-api
11+
12+
## Overview
13+
14+
This application consists of 3 parts:
15+
16+
- `api`, contains the Yii application for the REST API.
17+
- `console`, contains the Yii application for console commands, cronjobs or queues.
18+
- `backend`, contains the Yii application for a CRUD backend on the API data.
19+
20+

api/config/app-dev.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
/**
4+
* Main configuration file for the api application.
5+
*
6+
* Develop environment.
7+
*/
8+
9+
return array_merge(require(__DIR__ . '/app.php'), [
10+
'components' => array_merge(
11+
require __DIR__ . '/../../config/components.php', // common config
12+
require __DIR__ . '/../../config/components-dev.php', // common config (env overrides)
13+
require __DIR__ . '/components.php' // api specific config
14+
),
15+
]);

api/config/app-prod.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
/**
4+
* Main configuration file for the api application.
5+
*
6+
* Production environment.
7+
*/
8+
9+
return array_merge(require(__DIR__ . '/app.php'), [
10+
'components' => array_merge(
11+
require __DIR__ . '/../../config/components.php', // common config
12+
require __DIR__ . '/../../config/components-prod.php', // common config (env overrides)
13+
require __DIR__ . '/components.php' // api specific config
14+
),
15+
]);

api/config/app-test.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
/**
4+
* Main configuration file for the api application.
5+
*
6+
* Test environment.
7+
*/
8+
9+
return array_merge(require(__DIR__ . '/app.php'), [
10+
'components' => array_merge(
11+
require __DIR__ . '/../../config/components.php', // common config
12+
require __DIR__ . '/../../config/components-test.php', // common config (env overrides)
13+
require __DIR__ . '/components.php' // api specific config
14+
),
15+
]);

api/config/app.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
/**
4+
* Main configuration file for the api application.
5+
*/
6+
7+
return [
8+
'id' => 'api',
9+
10+
'basePath' => dirname(__DIR__),
11+
'vendorPath' => dirname(__DIR__, 2) . '/vendor',
12+
'runtimePath' => dirname(__DIR__, 2) . '/runtime/api',
13+
14+
'controllerNamespace' => 'api\controllers',
15+
16+
'bootstrap' => ['log'],
17+
18+
'params' => require(__DIR__ . '/../../config/params.php'),
19+
];

api/config/components.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
/**
4+
* component configuration overrides for the api application.
5+
*/
6+
7+
return [
8+
'request' => [
9+
'parsers' => [
10+
'application/json' => yii\web\JsonParser::class,
11+
],
12+
'enableCookieValidation' => false,
13+
],
14+
'response' => [
15+
'class' => yii\web\Response::class,
16+
'format' => yii\web\Response::FORMAT_JSON,
17+
],
18+
'urlManager' => [
19+
'enablePrettyUrl' => true,
20+
'enableStrictParsing' => true,
21+
'showScriptName' => false,
22+
'rules' => require(__DIR__ . '/url-rules.php'),
23+
],
24+
'log' => [
25+
'traceLevel' => YII_DEBUG ? 3 : 0,
26+
'targets' => [
27+
[
28+
'class' => yii\log\FileTarget::class,
29+
'levels' => ['error', 'warning'],
30+
'logFile' => '@logs/api-app.error.log',
31+
'except' => [
32+
'yii\web\HttpException*',
33+
],
34+
],
35+
[
36+
'class' => yii\log\FileTarget::class,
37+
'levels' => ['error', 'warning'],
38+
'logFile' => '@logs/api-http.error.log',
39+
'categories' => [
40+
'yii\web\HttpException*',
41+
],
42+
],
43+
[
44+
'class' => yii\log\FileTarget::class,
45+
'levels' => ['info'],
46+
'logFile' => '@logs/api-app.info.log',
47+
'logVars' => [],
48+
'except' => [
49+
'yii\db\*',
50+
],
51+
],
52+
],
53+
],
54+
];

api/config/url-rules.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
// TODO configure URL rules with autogenerated spec rules
4+
5+
return [
6+
'' => 'home/index',
7+
[
8+
'class' => yii\rest\UrlRule::class,
9+
'controller' => ['home'],
10+
'pluralize' => false,
11+
'extraPatterns' => [
12+
'GET index' => 'index'
13+
]
14+
]
15+
];

api/web/.htaccess

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
RewriteEngine on
2+
# If a directory or a file exists, use the request directly
3+
RewriteCond %{REQUEST_FILENAME} !-f
4+
RewriteCond %{REQUEST_FILENAME} !-d
5+
# Otherwise forward the request to index.php
6+
RewriteRule . index.php

api/web/index.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
require(__DIR__ . '/../../config/env.php');
4+
5+
$config = require(__DIR__ . '/../config/app-' . YII_ENV . '.php');
6+
7+
$app = new yii\web\Application($config);
8+
$app->run();

api/web/robots.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
User-agent: *
2+
Disallow: /

composer.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"require": {
3+
"php": ">=7.1.0",
4+
"cebe/assetfree-yii2": "~2.0.16",
5+
"cebe/php-openapi": "dev-master",
6+
"cebe/yii2-openapi": "dev-master",
7+
"yiisoft/yii2-gii": "~2.0.0 | ~2.1.0"
8+
},
9+
"autoload": {
10+
"psr-4": {
11+
"api\\": "api/",
12+
"backend\\": "backend/",
13+
"console\\": "console/",
14+
"common\\": "common/"
15+
}
16+
},
17+
"config": {
18+
"platform": {"php": "7.1.3"}
19+
}
20+
}

config/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/components-*.local.php
2+
!/components-ENV.local.php

config/components-ENV.local.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
/**
3+
* Template for local configuration adjustments.
4+
*
5+
* You can copy this file to the following environments to override component configurations locally:
6+
*
7+
* - components-dev.local.php
8+
* - components-prod.local.php
9+
* - components-test.local.php
10+
*/
11+
12+
return [
13+
'db' => [
14+
'class' => yii\db\Connection::class,
15+
'dsn' => 'mysql:host=localhost;dbname=api_db',
16+
'username' => 'root',
17+
'password' => '',
18+
'charset' => 'utf8',
19+
],
20+
];

config/components-dev.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
/**
4+
* General application components that are used in web, api and console application.
5+
*
6+
* Overrides for production environment.
7+
*/
8+
9+
return array_merge(
10+
[
11+
],
12+
file_exists($localConfig = __DIR__ . '/components-dev.local.php') ? require $localConfig : []
13+
);

config/components-prod.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
/**
4+
* General application components that are used in web, api and console application.
5+
*
6+
* Overrides for production environment.
7+
*/
8+
9+
return array_merge(
10+
[
11+
],
12+
file_exists($localConfig = __DIR__ . '/components-prod.local.php') ? require $localConfig : []
13+
);

config/components-test.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
/**
4+
* General application components that are used in web, api and console application.
5+
*
6+
* Overrides for test environment.
7+
*/
8+
9+
return array_merge([
10+
'mailer' => [
11+
'class' => \yii\swiftmailer\Mailer::class,
12+
'useFileTransport' => true,
13+
],
14+
],
15+
16+
file_exists($localConfig = __DIR__ . '/components-test.local.php') ? require $localConfig : []
17+
);

config/components.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
/**
4+
* General application components that are used in web and console application.
5+
*
6+
* Included by api/config/app.php, console/config/app.php and backend/config/app.php
7+
*/
8+
9+
return [
10+
'cache' => [
11+
'class' => yii\caching\FileCache::class,
12+
'cachePath' => '@root/runtime/cache',
13+
],
14+
];

config/env.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
if (!defined('YII_ENV')) {
4+
if (is_file($env = __DIR__ . '/../env.php')) {
5+
include($env);
6+
} else {
7+
define('YII_DEBUG', false);
8+
define('YII_ENV', 'prod');
9+
}
10+
}
11+
12+
require(__DIR__ . '/../vendor/autoload.php');
13+
require(__DIR__ . '/../vendor/yiisoft/yii2/Yii.php');
14+
15+
// register aliases
16+
Yii::setAlias('@root', __DIR__ . '/..');
17+
Yii::setAlias('@api', __DIR__ . '/../api');
18+
Yii::setAlias('@console', __DIR__ . '/../console');
19+
Yii::setAlias('@backend', __DIR__ . '/../backend');
20+
Yii::setAlias('@common', __DIR__ . '/../common');
21+
Yii::setAlias('@logs', __DIR__ . '/../logs');
22+
23+
require __DIR__ . '/events.php';

config/events.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
use yii\base\Event;
4+
5+
/**
6+
* register global event handlers here
7+
*/
8+
9+
//Event::on(MyClass::class, MyClass::EVENT_SOMETHING, function(Event $event) {
10+
// // ...
11+
//});

config/params.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
3+
return [];

console/config/app-dev.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
/**
4+
* Main configuration file for the console application.
5+
*
6+
* Develop environment.
7+
*/
8+
9+
return array_merge(require(__DIR__ . '/app.php'), [
10+
'components' => array_merge(
11+
require __DIR__ . '/../../config/components.php', // common config
12+
require __DIR__ . '/../../config/components-dev.php', // common config (env overrides)
13+
require __DIR__ . '/components.php' // console specific config
14+
),
15+
]);

console/config/app-prod.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
/**
4+
* Main configuration file for the console application.
5+
*
6+
* Production environment.
7+
*/
8+
9+
return array_merge(require(__DIR__ . '/app.php'), [
10+
'components' => array_merge(
11+
require __DIR__ . '/../../config/components.php', // common config
12+
require __DIR__ . '/../../config/components-prod.php', // common config (env overrides)
13+
require __DIR__ . '/components.php' // console specific config
14+
),
15+
]);

console/config/app-test.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
/**
4+
* Main configuration file for the console application.
5+
*
6+
* Test environment.
7+
*/
8+
9+
return array_merge(require(__DIR__ . '/app.php'), [
10+
'components' => array_merge(
11+
require __DIR__ . '/../../config/components.php', // common config
12+
require __DIR__ . '/../../config/components-test.php', // common config (env overrides)
13+
require __DIR__ . '/components.php' // console specific config
14+
),
15+
]);

0 commit comments

Comments
 (0)