Skip to content

Commit

Permalink
added backend application and Gii
Browse files Browse the repository at this point in the history
  • Loading branch information
cebe committed Mar 13, 2019
1 parent fd0ae1f commit 50ad603
Show file tree
Hide file tree
Showing 24 changed files with 1,269 additions and 10 deletions.
6 changes: 5 additions & 1 deletion .gitignore
@@ -1 +1,5 @@
/vendor
/vendor

/*/*.pid
/*/*.log
session_mm_*.sem
25 changes: 25 additions & 0 deletions Makefile
@@ -0,0 +1,25 @@

default:
@echo "The following commands are available:"
@echo ""
@echo " make start start PHP built-in webserver"
@echo " make stop stop PHP built-in webserver"

# start PHP built-in webserver
start:
@echo "Starting server for api"
cd api && $(MAKE) start
@echo "Starting server for backend"
cd backend && $(MAKE) start

# stop PHP built-in webserver
stop:
cd api && $(MAKE) stop
cd backend && $(MAKE) stop

test:
cd api && $(MAKE) test

clean: stop

.PHONY: start stop clean test
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -8,6 +8,7 @@ Yii Framework Application Template for quickly building API-first applications.

git clone https://github.com/cebe/yii2-app-api my-api
cd my-api
composer install

## Overview

Expand Down
38 changes: 38 additions & 0 deletions api/Makefile
@@ -0,0 +1,38 @@
# additional arguments to PHP binary
PHPARGS=
# enable XDEBUG for the test runner
XDEBUG=0
# enable XDEBUG for the webserver
XDEBUGW=0

WEBPORT=8337

ifeq ($(XDEBUG),1)
XDEBUGARGS=-dzend_extension=xdebug.so -dxdebug.remote_enable=1
endif
ifeq ($(XDEBUGW),1)
XDEBUGARGSW=-dzend_extension=xdebug.so -dxdebug.remote_enable=1
endif


current_dir=$(shell pwd)

default:

start: stop
@echo "Starting PHP webserver at http://localhost:$(WEBPORT), see php.log for access log."
@echo ""
@echo "Run make stop to stop it."
@echo ""
YII_ENV=dev php $(PHPARGS) $(XDEBUGARGSW) -S localhost:$(WEBPORT) -t web $(current_dir)/router.php >php.log 2>&1 & echo "$$!" > php.pid

stop:
@echo "Stopping PHP webserver..."
@-if [ -f php.pid ] ; then kill $$(cat php.pid); fi
@rm -f php.pid
@rm -f session_mm_*.sem

clean: stop
rm -f php.log

.PHONY: start stop clean
17 changes: 17 additions & 0 deletions api/router.php
@@ -0,0 +1,17 @@
<?php

if (getenv('YII_ENV')) {
define('YII_ENV', getenv('YII_ENV'));
define('YII_DEBUG', YII_ENV !== 'prod');
}

list($pathInfo) = explode('?', $_SERVER["REQUEST_URI"], 2);

if (is_file(__DIR__ . '/web/' . ltrim($pathInfo, '/'))) {
return false;
} else {
$_SERVER['SCRIPT_NAME'] = '/index.php';
$_SERVER['PHP_SELF'] = '/index.php';
$_SERVER['SCRIPT_FILENAME'] = __DIR__ . '/web/index.php';
include __DIR__ . '/web/index.php';
}
38 changes: 38 additions & 0 deletions backend/Makefile
@@ -0,0 +1,38 @@
# additional arguments to PHP binary
PHPARGS=
# enable XDEBUG for the test runner
XDEBUG=0
# enable XDEBUG for the webserver
XDEBUGW=0

WEBPORT=8338

ifeq ($(XDEBUG),1)
XDEBUGARGS=-dzend_extension=xdebug.so -dxdebug.remote_enable=1
endif
ifeq ($(XDEBUGW),1)
XDEBUGARGSW=-dzend_extension=xdebug.so -dxdebug.remote_enable=1
endif


current_dir=$(shell pwd)

default:

start: stop
@echo "Starting PHP webserver at http://localhost:$(WEBPORT), see php.log for access log."
@echo ""
@echo "Run make stop to stop it."
@echo ""
YII_ENV=dev php $(PHPARGS) $(XDEBUGARGSW) -S localhost:$(WEBPORT) -t web $(current_dir)/router.php >php.log 2>&1 & echo "$$!" > php.pid

stop:
@echo "Stopping PHP webserver..."
@-if [ -f php.pid ] ; then kill $$(cat php.pid); fi
@rm -f php.pid
@rm -f session_mm_*.sem

clean: stop
rm -f php.log

.PHONY: start stop clean
27 changes: 27 additions & 0 deletions backend/config/app-dev.php
@@ -0,0 +1,27 @@
<?php

/**
* Main configuration file for the backend application.
*
* Develop environment.
*/

$config = array_merge(require(__DIR__ . '/app.php'), [
'components' => array_merge(
require __DIR__ . '/../../config/components.php', // common config
require __DIR__ . '/../../config/components-dev.php', // common config (env overrides)
require __DIR__ . '/components.php' // backend specific config
),
]);

// enable Gii module
$config['bootstrap'][] = 'gii';
$config['modules']['gii'] = [
'class' => yii\gii\Module::class,
'generators' => [
// add ApiGenerator to Gii module
'api' => \cebe\yii2openapi\generator\ApiGenerator::class,
],
];

return $config;
15 changes: 15 additions & 0 deletions backend/config/app-prod.php
@@ -0,0 +1,15 @@
<?php

/**
* Main configuration file for the backend application.
*
* Production environment.
*/

return array_merge(require(__DIR__ . '/app.php'), [
'components' => array_merge(
require __DIR__ . '/../../config/components.php', // common config
require __DIR__ . '/../../config/components-prod.php', // common config (env overrides)
require __DIR__ . '/components.php' // backend specific config
),
]);
15 changes: 15 additions & 0 deletions backend/config/app-test.php
@@ -0,0 +1,15 @@
<?php

/**
* Main configuration file for the backend application.
*
* Test environment.
*/

return array_merge(require(__DIR__ . '/app.php'), [
'components' => array_merge(
require __DIR__ . '/../../config/components.php', // common config
require __DIR__ . '/../../config/components-test.php', // common config (env overrides)
require __DIR__ . '/components.php' // backend specific config
),
]);
22 changes: 22 additions & 0 deletions backend/config/app.php
@@ -0,0 +1,22 @@
<?php

/**
* Main configuration file for the backend application.
*/

return [
'id' => 'backend',

'basePath' => dirname(__DIR__),
'vendorPath' => dirname(__DIR__, 2) . '/vendor',
'runtimePath' => dirname(__DIR__, 2) . '/runtime/backend',
'aliases' => [
'@bower' => '@vendor/bower-asset',
'@npm' => '@vendor/npm-asset',
],
'controllerNamespace' => 'backend\controllers',

'bootstrap' => ['log'],

'params' => require(__DIR__ . '/../../config/params.php'),
];
48 changes: 48 additions & 0 deletions backend/config/components.php
@@ -0,0 +1,48 @@
<?php

/**
* component configuration overrides for the backend application.
*/

return [
'request' => [
'cookieValidationKey' => 'api1337', // TODO this should be dynamic
],

'urlManager' => [
'enablePrettyUrl' => true,
'enableStrictParsing' => true,
'showScriptName' => false,
'rules' => require(__DIR__ . '/url-rules.php'),
],
'log' => [
'traceLevel' => YII_DEBUG ? 3 : 0,
'targets' => [
[
'class' => yii\log\FileTarget::class,
'levels' => ['error', 'warning'],
'logFile' => '@logs/backend-app.error.log',
'except' => [
'yii\web\HttpException*',
],
],
[
'class' => yii\log\FileTarget::class,
'levels' => ['error', 'warning'],
'logFile' => '@logs/backend-http.error.log',
'categories' => [
'yii\web\HttpException*',
],
],
[
'class' => yii\log\FileTarget::class,
'levels' => ['info'],
'logFile' => '@logs/backend-app.info.log',
'logVars' => [],
'except' => [
'yii\db\*',
],
],
],
],
];
7 changes: 7 additions & 0 deletions backend/config/url-rules.php
@@ -0,0 +1,7 @@
<?php

// TODO configure URL rules with autogenerated spec rules

return [
'' => 'site/index',
];
16 changes: 16 additions & 0 deletions backend/controllers/SiteController.php
@@ -0,0 +1,16 @@
<?php

namespace backend\controllers;

use yii\web\Controller;

/**
*
*/
class SiteController extends Controller
{
public function actionIndex()
{
return $this->render('index');
}
}
17 changes: 17 additions & 0 deletions backend/router.php
@@ -0,0 +1,17 @@
<?php

if (getenv('YII_ENV')) {
define('YII_ENV', getenv('YII_ENV'));
define('YII_DEBUG', YII_ENV !== 'prod');
}

list($pathInfo) = explode('?', $_SERVER["REQUEST_URI"], 2);

if (is_file(__DIR__ . '/web/' . ltrim($pathInfo, '/'))) {
return false;
} else {
$_SERVER['SCRIPT_NAME'] = '/index.php';
$_SERVER['PHP_SELF'] = '/index.php';
$_SERVER['SCRIPT_FILENAME'] = __DIR__ . '/web/index.php';
include __DIR__ . '/web/index.php';
}
29 changes: 29 additions & 0 deletions backend/views/layouts/main.php
@@ -0,0 +1,29 @@
<?php

use yii\helpers\Html;

/** @var $this \yii\web\View */
/** @var $content string */

?>
<?php $this->beginPage() ?>
<!DOCTYPE html>
<html lang="<?= Yii::$app->language ?>">
<head>
<meta charset="<?= Yii::$app->charset ?>"/>
<meta name="viewport" content="width=device-width, initial-scale=1">
<?= Html::csrfMetaTags() ?>
<title><?= Html::encode(empty($this->title) ? '' : $this->title . ' - ') ?></title>
<?php $this->head() ?>
</head>
<body>
<?php $this->beginBody() ?>

<div class="wrap">
<?= $content ?>
</div>

<?php $this->endBody() ?>
</body>
</html>
<?php $this->endPage() ?>
10 changes: 10 additions & 0 deletions backend/views/site/index.php
@@ -0,0 +1,10 @@
<?php

use yii\helpers\Html;

/** @var $this \yii\web\View */

?>
<h1>API Backend</h1>

Go to <?= Html::a('/gii', ['/gii']) ?> to generate an API from OpenAPI spec.
6 changes: 6 additions & 0 deletions backend/web/.htaccess
@@ -0,0 +1,6 @@
RewriteEngine on
# If a directory or a file exists, use the request directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Otherwise forward the request to index.php
RewriteRule . index.php
2 changes: 2 additions & 0 deletions backend/web/assets/.gitignore
@@ -0,0 +1,2 @@
*
!.gitignore
8 changes: 8 additions & 0 deletions backend/web/index.php
@@ -0,0 +1,8 @@
<?php

require(__DIR__ . '/../../config/env.php');

$config = require(__DIR__ . '/../config/app-' . YII_ENV . '.php');

$app = new yii\web\Application($config);
$app->run();
2 changes: 2 additions & 0 deletions backend/web/robots.txt
@@ -0,0 +1,2 @@
User-agent: *
Disallow: /

0 comments on commit 50ad603

Please sign in to comment.