Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/268.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 我的RESTfulAPI YII2有什么问题?_Php_Api_Yii2 - Fatal编程技术网

Php 我的RESTfulAPI YII2有什么问题?

Php 我的RESTfulAPI YII2有什么问题?,php,api,yii2,Php,Api,Yii2,我的代码结构: config/ 控制器/ models/ api数据/ config/ api.php 模块/ v1/ 控制器/ ProgramStudiController.php module.php index.php .htaccess 下面是api.php: <?php $db = require(__DIR__ . '/../../config/db.php'); $params = require(__DIR__ . '/params

我的代码结构:

  • config/
  • 控制器/
  • models/
  • api数据/
    • config/
      • api.php
    • 模块/
      • v1/
        • 控制器/
          • ProgramStudiController.php
        • module.php
    • index.php
    • .htaccess
下面是
api.php

    <?php

$db     = require(__DIR__ . '/../../config/db.php');
$params = require(__DIR__ . '/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',
                ],
            ],
        ],
        'urlManager' => [
            'enablePrettyUrl' => true,
            'enableStrictParsing' => true,
            'showScriptName' => false,
            'rules' => [
                ['class' => 'yii\rest\UrlRule', 'controller' => 'v1/programstudi'],
            ],
        ], 
        'db' => $db,
    ],
    'modules' => [
        'v1' => [

            'basePath' => '@app/modules/v1',
            'class' => 'api-data\modules\v1\Module', // here is our v1 modules
            'controllerNamespace' => 'app\modules\v1\controllers',
        ],
    ],
    'params' => $params,
];

return $config;
 <?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();
    <?php
// Check this namespace:
namespace api-data\modules\v1;

class Module extends \yii\base\Module
{
    public function init()
    {
        parent::init();

        // ...  other initialization code ...
    }
}
    <?php
namespace \api-data\modules\v1\controllers;

use yii\rest\ActiveController;

class ProjectController extends ActiveController
{
    // We are using the regular web app modules:
    public $modelClass = 'app\models\ProgramStudi';
}
.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
module.php

    <?php

$db     = require(__DIR__ . '/../../config/db.php');
$params = require(__DIR__ . '/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',
                ],
            ],
        ],
        'urlManager' => [
            'enablePrettyUrl' => true,
            'enableStrictParsing' => true,
            'showScriptName' => false,
            'rules' => [
                ['class' => 'yii\rest\UrlRule', 'controller' => 'v1/programstudi'],
            ],
        ], 
        'db' => $db,
    ],
    'modules' => [
        'v1' => [

            'basePath' => '@app/modules/v1',
            'class' => 'api-data\modules\v1\Module', // here is our v1 modules
            'controllerNamespace' => 'app\modules\v1\controllers',
        ],
    ],
    'params' => $params,
];

return $config;
 <?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();
    <?php
// Check this namespace:
namespace api-data\modules\v1;

class Module extends \yii\base\Module
{
    public function init()
    {
        parent::init();

        // ...  other initialization code ...
    }
}
    <?php
namespace \api-data\modules\v1\controllers;

use yii\rest\ActiveController;

class ProjectController extends ActiveController
{
    // We are using the regular web app modules:
    public $modelClass = 'app\models\ProgramStudi';
}

我打了个电话,但电话号码是404。有人知道我做错了什么吗?

您将conroller命名为
ProgramstudicController
,然后命名为class
ProjectController
。重命名它并使用url,如
/api data/v1/program studi
,带有
-
符号,因为您在控制器名称中使用了camelcase


顺便说一句,您应该在
controllers/
文件夹中创建主控制器,然后在
api data/modules/v1/controllers
中扩展它。嵌套项目符号是显示目录树的一种好方法。你可以在每个项目前面加上
*
,并在每个级别缩进四个空格。OK谢谢你的信息,我是这里的新手谢谢你的回答,你能帮我举一些主控制器的例子吗,im是yii2中的新手吗?主控制器是正常的控制器,你把所有动作都放在这里,然后只需在
api data/v1/controllers
中扩展此控制器,这样您就可以从主控制器继承所有方法,如果需要,您可以重写(在这种情况下,您不需要这个,它只是应用程序逻辑)