-
Notifications
You must be signed in to change notification settings - Fork 129
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
577e3a1
commit 1115374
Showing
10 changed files
with
184 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
], | ||
], | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>', | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Error |