Yii api路由返回404

nnvyjq4y  于 2022-11-09  发布在  其他
关注(0)|答案(1)|浏览(129)

我的项目有一个api文件夹,它与frontend和backend文件夹在同一层。
这是我的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,但我得到的都是来自apache 2的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响应,它说Route无效,无法解析请求:

  • http://myserver/myproject/api/web/index.php/演示文稿/索引
f5emj3cl

f5emj3cl1#

在Yii高级模板中,如果你想要后端和前端在同一级别上,你必须使用不同的主机名。
您必须在链接到已启用站点的目录的目录/etc/apache 2/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目录,特别是文件.htaccessindex.php,以使用您的api层。

相关问题