Yii api 路线 return 404
Yii api routes return 404
我的项目有一个 api 文件夹,与前端和后端文件夹处于同一级别。
这是我的 api/config/main.php 文件:
<?php
if (isset($_SERVER['APPLICATION_ENV'])) {
$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')
);
} else {
$params = array_merge(
require(__DIR__ . '/../../common/config/params.php'),
require(__DIR__ . '/params.php')
);
}
return [
'id' => 'app-api',
'basePath' => dirname(__DIR__),
'bootstrap' => ['log'],
'components' => [
'response' => [
'format' => yii\web\Response::FORMAT_JSON,
'charset' => 'UTF-8',
],
'request' => [
'enableCookieValidation' => true,
'enableCsrfValidation' => true,
'cookieValidationKey' => 'uwu',
],
'user' => [
'identityClass' => 'common\modules\user\models\User',
'enableAutoLogin' => false,
],
'log' => [
'traceLevel' => YII_DEBUG ? 3 : 0,
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning'],
],
],
],
'urlManager' => [
'enablePrettyUrl' => true,
'enableStrictParsing' => false,
'showScriptName' => false,
'rules' => [
[
'class' => 'yii\rest\UrlRule',
'controller' => 'v1/user',
'pluralize' => false
],
[
'class' => 'yii\rest\UrlRule',
'controller' => 'v1/code',
'pluralize' => false
]
],
]
],
'modules' => [
'v1' => [
'basePath' => '@api/modules/v1',
'class' => 'api\modules\v1\Module',
'controllerNamespace' => 'api\modules\v1\controllers'
]
],
'params' => $params,
];
我已经尝试访问这些不同的 url,但我得到的只是来自 apache2 的 404 Not Found 响应:
http://myserver/myproject/api/web/presentation/index
http://myserver/myproject/api/presentation/index
http://myserver/myproject/v1/presentation/index
http://myserver/myproject/v1/web/presentation/index
http://myserver/myproject/api/v1/presentation/index
http://myserver/myproject/api/v1/web/presentation/index
然后 URL 我得到 Yii 生成的 404 Not found JSON 响应,表示路由无效且无法解析请求:
http://myserver/myproject/api/web/index.php
/presentation/index
在 Yii 高级模板中,如果你想要与后端和前端处于同一级别的东西,你必须使用不同的主机名。
您必须在链接到站点启用目录的目录 /etc/apache2/sites-available 中的 apache 配置中定义虚拟主机:
<VirtualHost *:80>
DocumentRoot /YOUR_PROJECT_DIR/api/web
ServerName YOUR.SERVER
<Directory /YOUR_PROJECT_DIR/api/web>
AllowOverride All
Order allow,deny
allow from all
</Directory>
</VirtualHost>
进一步分析例如frontend/web 目录,特别是文件 .htaccess
和 index.php
以使用您的 api
等级。
我的项目有一个 api 文件夹,与前端和后端文件夹处于同一级别。
这是我的 api/config/main.php 文件:
<?php
if (isset($_SERVER['APPLICATION_ENV'])) {
$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')
);
} else {
$params = array_merge(
require(__DIR__ . '/../../common/config/params.php'),
require(__DIR__ . '/params.php')
);
}
return [
'id' => 'app-api',
'basePath' => dirname(__DIR__),
'bootstrap' => ['log'],
'components' => [
'response' => [
'format' => yii\web\Response::FORMAT_JSON,
'charset' => 'UTF-8',
],
'request' => [
'enableCookieValidation' => true,
'enableCsrfValidation' => true,
'cookieValidationKey' => 'uwu',
],
'user' => [
'identityClass' => 'common\modules\user\models\User',
'enableAutoLogin' => false,
],
'log' => [
'traceLevel' => YII_DEBUG ? 3 : 0,
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning'],
],
],
],
'urlManager' => [
'enablePrettyUrl' => true,
'enableStrictParsing' => false,
'showScriptName' => false,
'rules' => [
[
'class' => 'yii\rest\UrlRule',
'controller' => 'v1/user',
'pluralize' => false
],
[
'class' => 'yii\rest\UrlRule',
'controller' => 'v1/code',
'pluralize' => false
]
],
]
],
'modules' => [
'v1' => [
'basePath' => '@api/modules/v1',
'class' => 'api\modules\v1\Module',
'controllerNamespace' => 'api\modules\v1\controllers'
]
],
'params' => $params,
];
我已经尝试访问这些不同的 url,但我得到的只是来自 apache2 的 404 Not Found 响应:
http://myserver/myproject/api/web/presentation/index
http://myserver/myproject/api/presentation/index
http://myserver/myproject/v1/presentation/index
http://myserver/myproject/v1/web/presentation/index
http://myserver/myproject/api/v1/presentation/index
http://myserver/myproject/api/v1/web/presentation/index
然后 URL 我得到 Yii 生成的 404 Not found JSON 响应,表示路由无效且无法解析请求:
http://myserver/myproject/api/web/index.php
/presentation/index
在 Yii 高级模板中,如果你想要与后端和前端处于同一级别的东西,你必须使用不同的主机名。
您必须在链接到站点启用目录的目录 /etc/apache2/sites-available 中的 apache 配置中定义虚拟主机:
<VirtualHost *:80>
DocumentRoot /YOUR_PROJECT_DIR/api/web
ServerName YOUR.SERVER
<Directory /YOUR_PROJECT_DIR/api/web>
AllowOverride All
Order allow,deny
allow from all
</Directory>
</VirtualHost>
进一步分析例如frontend/web 目录,特别是文件 .htaccess
和 index.php
以使用您的 api
等级。