Please i used this code for creating my DB API but i don’t know what is wrong. Please i need some help
API Structure
basic
- web
- config
- controllers
… - api
- config
- modules
- v1
- controllers
.htaccess
index.php
- controllers
- v1
Index.php
// 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();
.htaccess
Options +FollowSymLinks
IndexIgnore /
RewriteEngine on
if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
otherwise forward it to index.php
RewriteRule . index.php
api.php (in api/config folder)
$db = require(DIR . ‘/…/…/config/db.php’);
$params = require(DIR . ‘/…/…/config/params.php’);
$config = [
‘id’ => ‘basic’,
//‘id’ => ‘app-api’, //ajouter
‘name’ => ‘TimeTracker’,
// Need to get one level up:
'basePath' => dirname(__DIR__).'/..',
//'basePath' => dirname(__DIR__), //ajouter
'controllerNamespace' => 'api\controllers', //ajouter
'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',
],
],
],
'urlManager' => [
'enablePrettyUrl' => true,
'enableStrictParsing' => true,
'showScriptName' => false,
'rules' => [
['class' => 'yii\rest\UrlRule', 'controller' => ['v1/users','v1/time']],
],
],
'db' => $db,
'users' => [
'identityClass' => 'app\models\Users',
'enableAutoLogin' => false,
'enableSession' => false,
'loginUrl' => null
],
],
'modules' => [
'v1' => [
'class' => 'app\api\modules\v1\Module',
],
],
'params' => $params,
];
return $config;
UsersController.php (in api/modules/v1/controllers folder)
namespace app\api\modules\v1\controllers;
use yii\rest\ActiveController;
class UsersController extends ActiveController
{
// We are using the regular web app modules:
public $modelClass = ‘app\models\Users’;
}
Module.php (in api/modules/v1 folder)
// Check this namespace:
namespace app\api\modules\v1;
class Module extends \yii\base\Module
{
public function init()
{
parent::init();
\Yii::$app->user->enableSession = false;
}
}
Users.php (in basic/models folder, générated by gii)
namespace app\models;
use Yii;
/**
-
This is the model class for table “users”.
-
@property int $id
-
@property string $login
-
@property string $password
-
@property int $status
/
class Users extends \yii\db\ActiveRecord
{
/*- {@inheritdoc}
*/
public static function tableName()
{
return ‘users’;
}
/**
- {@inheritdoc}
*/
public function rules()
{
return [
[[‘login’, ‘password’, ‘status’], ‘required’],
[[‘status’], ‘integer’],
[[‘login’, ‘password’], ‘string’, ‘max’ => 100],
];
}
/**
- {@inheritdoc}
*/
public function attributeLabels()
{
return [
‘id’ => ‘ID’,
‘login’ => ‘Login’,
‘password’ => ‘Password’,
‘status’ => ‘Status’,
];
}
}
- {@inheritdoc}
db.php (in basic/config folder,)
return [
‘class’ => ‘yii\db\Connection’,
//‘dsn’ => ‘mysql:host=localhost;dbname=yii2basic’,
‘dsn’ => ‘mysql:host=localhost;dbname=roi_plateforme’,
‘username’ => ‘root’,
‘password’ => ‘’,
‘charset’ => ‘utf8’,
// Schema cache options (for production environment)
//'enableSchemaCache' => true,
//'schemaCacheDuration' => 60,
//'schemaCache' => 'cache',
];
I tested it with postman using get method like this “http://localhost:8081/api/users”