Yii2在高級模板中設置API的應用程序時遇到問題。我不能讓任何輸入,但一個404呼叫我的API的V1模塊yii2 API 404 - 高級模板
我試圖打電話與地址的API: http://localhost/HeyConference/api/v1/country
我有以下教程/模板:
- http://www.yiiframework.com/forum/index.php/topic/69123-error-while-creating-a-rest-api-in-yii2-advanced-template-closed/
- http://budiirawan.com/setup-restful-api-yii2/(這裏Module.php檔案遺失)
爲w請求支持同樣的問題,但沒有成功。
Yii的版本:2.0.10 PHP版本:23年6月5日 和 PHP版本:5.5(XAMP)
我懷疑問題出在我的配置
我的文件結構是:
- _protected
- API
- 配置
- bootstrap.php中
- main.php
- 主+ local.php
- params.php
- PARAMS + local.php
- 控制器
- 個SiteController.php
- 模塊
- V1
- 控制器
- Contry.php
- 模型
- Country.php
- 模塊。PHP
- 控制器
- V1
- 運行
- 緩存
- 調試
- 日誌
- 配置
- API
- ....
- API
- 資產
- 的.htaccess
- 的index.php
- 指數
- test.php的
- 的robots.txt
這裏有特定的文件:
的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 /modules/v1/controllers/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
你怎麼罵返回404錯誤的行動? – scaisEdge
也許你忘了.htaccess文件? – Vitaly