Код: Выделить всё
// comment out the following two lines when deployed to production
defined('YII_DEBUG') or define('YII_DEBUG', true);
defined('YII_ENV') or define('YII_ENV', 'dev');
require(__DIR__ . '/../../vendor/autoload.php');
require(__DIR__ . '/../../vendor/yiisoft/yii2/Yii.php');
// Use a distinct configuration for the API
$config = require(__DIR__ . '/../config/api.php');
(new yii\web\Application($config))->run();
Код: Выделить всё
<?php
$db = require(__DIR__ . '/../../config/db.php');
$params = require(__DIR__ . '/../../config/params.php');
$config = [
'id' => 'basic',
'name' => 'TimeTracker',
// Need to get one level up:
'basePath' => dirname(__DIR__).'/../',
'bootstrap' => ['log'],
'components' => [
'request' => [
// Enable JSON Input:
'parsers' => [
'application/json' => 'yii\web\JsonParser',
]
],
'log' => [
'traceLevel' => YII_DEBUG ? 3 : 0,
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning'],
// Create API log in the standard log dir
// But in file 'api.log':
'logFile' => '@app/runtime/logs/api.log',
],
],
],
'cache' => [
'class' => 'yii\caching\FileCache',
],
'urlManager' => [
'enablePrettyUrl' => true,
'enableStrictParsing' => true,
'showScriptName' => false,
'rules' => [
[
'class' => 'yii\rest\UrlRule',
'controller' => ['v1/project','v1/time']
],
],
],
'db' => $db,
'user' => [
'identityClass' => 'app\models\User',
'enableAutoLogin' => false,
],
],
'modules' => [
'v1' => [
'class' => 'app\api\modules\v1\Module',
],
],
'params' => $params,
];
return $config;
Код: Выделить всё
<?php
// Check this namespace:
namespace app\api\modules\v1;
class Module extends \yii\base\Module
{
public $controllerNamespace = 'app\api\modules\v1\controllers';
public function init()
{
parent::init();
// ... other initialization code ...
}
}
Код: Выделить всё
<?php
namespace app\api\modules\v1\controllers;
use yii\rest\ActiveController;
class ProjectController extends ActiveController
{
// We are using the regular web app modules:
public $modelClass = 'app\models\Project';
public function actionGet() {
return [
'id' => 1,
];
}
}