Skip to content

Commit 50ad603

Browse files
committed
added backend application and Gii
1 parent fd0ae1f commit 50ad603

24 files changed

+1269
-10
lines changed

.gitignore

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1-
/vendor
1+
/vendor
2+
3+
/*/*.pid
4+
/*/*.log
5+
session_mm_*.sem

Makefile

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
default:
3+
@echo "The following commands are available:"
4+
@echo ""
5+
@echo " make start start PHP built-in webserver"
6+
@echo " make stop stop PHP built-in webserver"
7+
8+
# start PHP built-in webserver
9+
start:
10+
@echo "Starting server for api"
11+
cd api && $(MAKE) start
12+
@echo "Starting server for backend"
13+
cd backend && $(MAKE) start
14+
15+
# stop PHP built-in webserver
16+
stop:
17+
cd api && $(MAKE) stop
18+
cd backend && $(MAKE) stop
19+
20+
test:
21+
cd api && $(MAKE) test
22+
23+
clean: stop
24+
25+
.PHONY: start stop clean test

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Yii Framework Application Template for quickly building API-first applications.
88

99
git clone https://github.com/cebe/yii2-app-api my-api
1010
cd my-api
11+
composer install
1112

1213
## Overview
1314

api/Makefile

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# additional arguments to PHP binary
2+
PHPARGS=
3+
# enable XDEBUG for the test runner
4+
XDEBUG=0
5+
# enable XDEBUG for the webserver
6+
XDEBUGW=0
7+
8+
WEBPORT=8337
9+
10+
ifeq ($(XDEBUG),1)
11+
XDEBUGARGS=-dzend_extension=xdebug.so -dxdebug.remote_enable=1
12+
endif
13+
ifeq ($(XDEBUGW),1)
14+
XDEBUGARGSW=-dzend_extension=xdebug.so -dxdebug.remote_enable=1
15+
endif
16+
17+
18+
current_dir=$(shell pwd)
19+
20+
default:
21+
22+
start: stop
23+
@echo "Starting PHP webserver at http://localhost:$(WEBPORT), see php.log for access log."
24+
@echo ""
25+
@echo "Run make stop to stop it."
26+
@echo ""
27+
YII_ENV=dev php $(PHPARGS) $(XDEBUGARGSW) -S localhost:$(WEBPORT) -t web $(current_dir)/router.php >php.log 2>&1 & echo "$$!" > php.pid
28+
29+
stop:
30+
@echo "Stopping PHP webserver..."
31+
@-if [ -f php.pid ] ; then kill $$(cat php.pid); fi
32+
@rm -f php.pid
33+
@rm -f session_mm_*.sem
34+
35+
clean: stop
36+
rm -f php.log
37+
38+
.PHONY: start stop clean

api/router.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
if (getenv('YII_ENV')) {
4+
define('YII_ENV', getenv('YII_ENV'));
5+
define('YII_DEBUG', YII_ENV !== 'prod');
6+
}
7+
8+
list($pathInfo) = explode('?', $_SERVER["REQUEST_URI"], 2);
9+
10+
if (is_file(__DIR__ . '/web/' . ltrim($pathInfo, '/'))) {
11+
return false;
12+
} else {
13+
$_SERVER['SCRIPT_NAME'] = '/index.php';
14+
$_SERVER['PHP_SELF'] = '/index.php';
15+
$_SERVER['SCRIPT_FILENAME'] = __DIR__ . '/web/index.php';
16+
include __DIR__ . '/web/index.php';
17+
}

backend/Makefile

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# additional arguments to PHP binary
2+
PHPARGS=
3+
# enable XDEBUG for the test runner
4+
XDEBUG=0
5+
# enable XDEBUG for the webserver
6+
XDEBUGW=0
7+
8+
WEBPORT=8338
9+
10+
ifeq ($(XDEBUG),1)
11+
XDEBUGARGS=-dzend_extension=xdebug.so -dxdebug.remote_enable=1
12+
endif
13+
ifeq ($(XDEBUGW),1)
14+
XDEBUGARGSW=-dzend_extension=xdebug.so -dxdebug.remote_enable=1
15+
endif
16+
17+
18+
current_dir=$(shell pwd)
19+
20+
default:
21+
22+
start: stop
23+
@echo "Starting PHP webserver at http://localhost:$(WEBPORT), see php.log for access log."
24+
@echo ""
25+
@echo "Run make stop to stop it."
26+
@echo ""
27+
YII_ENV=dev php $(PHPARGS) $(XDEBUGARGSW) -S localhost:$(WEBPORT) -t web $(current_dir)/router.php >php.log 2>&1 & echo "$$!" > php.pid
28+
29+
stop:
30+
@echo "Stopping PHP webserver..."
31+
@-if [ -f php.pid ] ; then kill $$(cat php.pid); fi
32+
@rm -f php.pid
33+
@rm -f session_mm_*.sem
34+
35+
clean: stop
36+
rm -f php.log
37+
38+
.PHONY: start stop clean

backend/config/app-dev.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
/**
4+
* Main configuration file for the backend application.
5+
*
6+
* Develop environment.
7+
*/
8+
9+
$config = 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' // backend specific config
14+
),
15+
]);
16+
17+
// enable Gii module
18+
$config['bootstrap'][] = 'gii';
19+
$config['modules']['gii'] = [
20+
'class' => yii\gii\Module::class,
21+
'generators' => [
22+
// add ApiGenerator to Gii module
23+
'api' => \cebe\yii2openapi\generator\ApiGenerator::class,
24+
],
25+
];
26+
27+
return $config;

backend/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 backend 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' // backend specific config
14+
),
15+
]);

backend/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 backend 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' // backend specific config
14+
),
15+
]);

backend/config/app.php

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

backend/config/components.php

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

backend/config/url-rules.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
// TODO configure URL rules with autogenerated spec rules
4+
5+
return [
6+
'' => 'site/index',
7+
];
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace backend\controllers;
4+
5+
use yii\web\Controller;
6+
7+
/**
8+
*
9+
*/
10+
class SiteController extends Controller
11+
{
12+
public function actionIndex()
13+
{
14+
return $this->render('index');
15+
}
16+
}

backend/router.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
if (getenv('YII_ENV')) {
4+
define('YII_ENV', getenv('YII_ENV'));
5+
define('YII_DEBUG', YII_ENV !== 'prod');
6+
}
7+
8+
list($pathInfo) = explode('?', $_SERVER["REQUEST_URI"], 2);
9+
10+
if (is_file(__DIR__ . '/web/' . ltrim($pathInfo, '/'))) {
11+
return false;
12+
} else {
13+
$_SERVER['SCRIPT_NAME'] = '/index.php';
14+
$_SERVER['PHP_SELF'] = '/index.php';
15+
$_SERVER['SCRIPT_FILENAME'] = __DIR__ . '/web/index.php';
16+
include __DIR__ . '/web/index.php';
17+
}

backend/views/layouts/main.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
use yii\helpers\Html;
4+
5+
/** @var $this \yii\web\View */
6+
/** @var $content string */
7+
8+
?>
9+
<?php $this->beginPage() ?>
10+
<!DOCTYPE html>
11+
<html lang="<?= Yii::$app->language ?>">
12+
<head>
13+
<meta charset="<?= Yii::$app->charset ?>"/>
14+
<meta name="viewport" content="width=device-width, initial-scale=1">
15+
<?= Html::csrfMetaTags() ?>
16+
<title><?= Html::encode(empty($this->title) ? '' : $this->title . ' - ') ?></title>
17+
<?php $this->head() ?>
18+
</head>
19+
<body>
20+
<?php $this->beginBody() ?>
21+
22+
<div class="wrap">
23+
<?= $content ?>
24+
</div>
25+
26+
<?php $this->endBody() ?>
27+
</body>
28+
</html>
29+
<?php $this->endPage() ?>

backend/views/site/index.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
use yii\helpers\Html;
4+
5+
/** @var $this \yii\web\View */
6+
7+
?>
8+
<h1>API Backend</h1>
9+
10+
Go to <?= Html::a('/gii', ['/gii']) ?> to generate an API from OpenAPI spec.

backend/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

backend/web/assets/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*
2+
!.gitignore

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

backend/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: /

0 commit comments

Comments
 (0)