我在Yii2中在高级模板中设置API应用程序时遇到问题。我无法获得任何输入,但404使用我的API的v1调用模块
我试图用地址调用api: http://localhost/HeyConference/api/v1/country
我有以下教程/模板:
以及对同一问题的支持请求,但没有成功。
Yii版本:2.0.10 PHP版本:5.6.23 和 PHP版本:5.5(XAMP)
我怀疑问题出在我的配置中
我的文件结构是:
这里是特定文件:
的index.php
<?php
defined('YII_DEBUG') or define('YII_DEBUG', true);
defined('YII_ENV') or define('YII_ENV', 'dev');
require(__DIR__ . '/../_protected/vendor/autoload.php');
require(__DIR__ . '/../_protected/vendor/yiisoft/yii2/Yii.php');
require(__DIR__ . '/../_protected/common/config/bootstrap.php');
require(__DIR__ . '/../_protected/api/config/bootstrap.php');
$config = yii\helpers\ArrayHelper::merge(
require(__DIR__ . '/../_protected/common/config/main.php'),
require(__DIR__ . '/../_protected/common/config/main-local.php'),
require(__DIR__ . '/../_protected/api/config/main.php'),
require(__DIR__ . '/../_protected/api/config/main-local.php')
);
$application = new yii\web\Application($config);
$application->run();
_protected / API /配置/ main.php
<?php
$params = array_merge(
require(__DIR__ . '/../../common/config/params.php'),
require(__DIR__ . '/../../common/config/params-local.php'),
require(__DIR__ . '/params.php'),
require(__DIR__ . '/params-local.php')
);
return [
'id' => 'app-api',
'basePath' => dirname(__DIR__),
//'controllerNamespace' => 'api\controllers',
//'controllerNamespace' => 'api\modules\v1\controllers',
'bootstrap' => ['log'],
'modules' => [
'v1' => [
'basePath' => '@api/modules/v1',
'class' => 'api\modules\v1\Module', // here is our v1 modules
'controllerNamespace' => 'api\modules\v1\controllers',
]
],
'components' => [
'user' => [
'identityClass' => 'common\models\UserIdentity',
'enableAutoLogin' => false,
],
'log' => [
'traceLevel' => YII_DEBUG ? 3 : 0,
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning'],
],
],
],
'urlManager' => [
'enablePrettyUrl' => true,
'enableStrictParsing' => false, //true,
'showScriptName' => false,
'rules' => [
[
//'pluralize' => false,
'class' => 'yii\rest\UrlRule',
'controller' => ['v1/country', 'v1/conference'],
'tokens' => [
'{id}' => '<id:\\w+>'
]
]
],
]
],
'params' => $params,
];
_protected / API /模块/ V1 / Module.php
<?php
namespace api\modules\v1;
class Module extends \yii\base\Module
{
public $controllerNamespace = 'api\modules\v1\controllers';
public function init()
{
parent::init();
}
}
_protected / API /模块/ V1 /控制器/ Country.php
<?php
namespace api\modules\v1\controllers;
use yii\rest\ActiveController;
class ConferenceController extends ActiveController
{
public $modelClass = 'api\modules\v1\models\Conference';
}
_protected / API /模块/ V1 /控制器/ Country.php
<?php
namespace api\modules\v1\controllers;
use yii\rest\ActiveController;
/**
* Country Controller API
*
* @author alex
*/
class CountryController extends ActiveController
{
public $modelClass = 'api\modules\v1\models\Country';
}
.htaccess文件:
RewriteEngine on
# If a directory or a file exists, use the request directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Otherwise forward the request to index.php
RewriteRule . index.php
答案 0 :(得分:0)
我看到你的控制器没有遵循Yii2惯例。 它们应该被称为 XXX 控制器。
因此,您的文件应命名为_protected/api/modules/v1/controllers/Country.php
_protected/api/modules/v1/controllers/CountryController.php