Skip to content

Commit

Permalink
add draft for api module
Browse files Browse the repository at this point in the history
  • Loading branch information
grandmotivator committed Jun 11, 2024
1 parent 577e3a1 commit 1115374
Show file tree
Hide file tree
Showing 10 changed files with 184 additions and 0 deletions.
4 changes: 4 additions & 0 deletions config/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
'support-groups/clients-list/<id:[\d]+>' => 'support-groups/clients-list',
'support-groups/clients-view/<id:[\d]+>' => 'support-groups/clients-view',

'api/<controller:[-\w]+>/<action:[-\w]+>/<id:[\d]+>' => '<controller>/<action>',
'api/<controller:[-\w]+>/<action:[-\w]+>/' => '<controller>/<action>',
'api/<controller:[-\w]+>/<action:[-\w]+>' => '<controller>/<action>',

'<controller:[-\w]+>/<action:[-\w]+>/<id:[\d]+>' => '<controller>/<action>',
'<controller:[-\w]+>/<action:[-\w]+>/' => '<controller>/<action>',
'<controller:[-\w]+>/<action:[-\w]+>' => '<controller>/<action>',
Expand Down
4 changes: 4 additions & 0 deletions config/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
'@bower' => '@vendor/bower-asset',
'@npm' => '@vendor/npm-asset',
'@bot' => '@app/modules/bot',
'@api' => '@app/modules/api',
],
'modules' => [
'guest' => [
Expand All @@ -41,6 +42,9 @@
'bot' => [
'class' => 'app\modules\bot\Module',
],
'api' => [
'class' => 'app\modules\api\Module',
],
],
'components' => [
'assetManager' => [
Expand Down
35 changes: 35 additions & 0 deletions modules/api/Module.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace app\modules\api;

use Yii;

/**
* OSW API module definition class
*
* @link https://opensourcewebsite.org/api
* @link https://apidocs.opensourcewebsite.org
*/
class Module extends \yii\base\Module
{
/**
* {@inheritdoc}
*/
public $controllerNamespace = 'app\modules\api\controllers';

/**
* {@inheritdoc}
*/
public function init()
{
parent::init();

Yii::configure($this, require_once __DIR__ . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'common.php');

$handler = $this->get('errorHandler');

Yii::$app->set('errorHandler', $handler);

$handler->register();
}
}
22 changes: 22 additions & 0 deletions modules/api/components/Controller.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace app\modules\api\components;

use Yii;

/**
* Class Controller
*
* @package app\modules\api
*/
class Controller extends \yii\rest\Controller
{
public function behaviors()
{
return [
'corsFilter' => [
'class' => \yii\filters\Cors::class,
],
];
}
}
52 changes: 52 additions & 0 deletions modules/api/config/common.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

return [
'id' => 'api',
'layout' => false,
'defaultRoute' => 'default/index',
'components' => [
'urlManager' => [
'class' => 'yii\web\UrlManager',
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => require (__DIR__ . '/routes.php'),
],
'response' => [
'class' => 'yii\web\Response',
'on beforeSend' => function ($event) {
$response = $event->sender;

if ($response->data !== null) {
$response->data = [
'success' => $response->isSuccessful,
'data' => $response->data,
];

$response->statusCode = 200;
}
},
],
'cors' => [
'class' => 'yii\filters\Cors',
'cors' => [
// Allow sources (domains)
'Origin' => ['*'],
// Allow methods
'Access-Control-Request-Method' => ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'HEAD', 'OPTIONS'],
// Allow headers
'Access-Control-Request-Headers' => ['*'],
// Allow credentials (cookies, authorization headers, etc.) to be exposed to the browser
'Access-Control-Allow-Credentials' => true,
// Allow OPTIONS caching, sec
'Access-Control-Max-Age' => 60 * 60,
// Allow the X-Pagination-Current-Page header to be exposed to the browser
'Access-Control-Expose-Headers' => ['X-Pagination-Current-Page'],
],
],
// TODO error handler
'errorHandler' => [
'class' => 'yii\web\errorHandler',
'errorAction' => 'api/error/index',
],
],
];
7 changes: 7 additions & 0 deletions modules/api/config/routes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

return [
'<controller:[-\w]+>/<action:[-\w]+>/<id:[\d]+>' => '<controller>/<action>',
'<controller:[-\w]+>/<action:[-\w]+>/' => '<controller>/<action>',
'<controller:[-\w]+>/<action:[-\w]+>' => '<controller>/<action>',
];
20 changes: 20 additions & 0 deletions modules/api/controllers/DefaultController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace app\modules\api\controllers;

use app\modules\api\components\Controller;

/**
* Default controller for the `api` module
*/
class DefaultController extends Controller
{
/**
* Renders the index view for the module
* @return string
*/
public function actionIndex()
{
return $this->render('index');
}
}
27 changes: 27 additions & 0 deletions modules/api/controllers/ErrorController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace app\modules\api\controllers;

use app\modules\api\components\Controller;
use Yii;

/**
* Error controller for the `api` module
*/
class ErrorController extends Controller
{
/**
* Renders the index view for the module
* @return string
*/
public function actionIndex()
{
$exception = Yii::$app->errorHandler->exception;

if ($exception !== null) {
return $this->render('index', ['exception' => $exception]);
}

return $this->render('index');
}
}
12 changes: 12 additions & 0 deletions modules/api/views/default/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<div class="api-default-index">
<h1><?= $this->context->action->uniqueId ?></h1>
<p>
This is the view content for action "<?= $this->context->action->id ?>".
The action belongs to the controller "<?= get_class($this->context) ?>"
in the "<?= $this->context->module->id ?>" module.
</p>
<p>
You may customize this page by editing the following file:<br>
<code><?= __FILE__ ?></code>
</p>
</div>
1 change: 1 addition & 0 deletions modules/api/views/error/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Error

0 comments on commit 1115374

Please sign in to comment.