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();

0 commit comments

Comments
 (0)