【问题标题】:404 Not Found for API routes in YiiYii 中的 API 路由 404 Not Found
【发布时间】:2017-01-19 19:42:28
【问题描述】:

我正在尝试在本地实现另一个开发人员创建的 Yii 项目,我正在 Mac 上运行 Xampp 5.6.28。

目前该项目正在为后端和前端 url 提供服务,但 api 路由抛出 404 Not Found 错误。

Apache conf 是:

<Directory "/Applications/XAMPP/xamppfiles/htdocs">
    # use mod_rewrite for pretty URL support
    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

    # XAMPP
    Options Indexes FollowSymLinks ExecCGI Includes

    #
    # AllowOverride controls what directives may be placed in .htaccess files.
    # It can be "All", "None", or any combination of the keywords:
    #   Options FileInfo AuthConfig Limit
    #
    #AllowOverride None
    # since XAMPP 1.4:
    AllowOverride All

    #
    # Controls who can get stuff from this server.
    #
    Require all granted
</Directory>

应用遵循“Yii 2 Advanced Project Template”结构,api在: app/api/ 和应该响应的控制器位于:app/api/modules/v1/controllers/AppController.php

有一个 api/config/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',
    'defaultRoute'=>'default',
    'bootstrap' => ['log'],
    'modules' => [
        'v1' => [
            'class' => 'api\modules\v1\Module',
        ],
    ],
    'components' => [
        'user' => [
            'identityClass' => 'common\models\User',
            'enableAutoLogin' => false,
        ],
        'log' => [
            'traceLevel' => YII_DEBUG ? 3 : 0,
            'targets' => [
                [
                    'class' => 'yii\log\FileTarget',
                    'levels' => ['error', 'warning'],
                ],
            ],
        ],
        'request' => [
                'enableCsrfValidation'=>false,
                'parsers' => [
                        'application/json' => 'yii\web\JsonParser',
                ]
        ],
        'errorHandler' => [
                'errorAction' => 'default/error',
        ],
        'urlManager'=>[
                'enablePrettyUrl'=>true,
                'showScriptName'=>false,
        ],
    ],
    'params' => $params,
];

还有一个 common/config/main.php :

<?php
return [
    'vendorPath' => dirname(dirname(__DIR__)) . '/vendor',
    'name' => 'FULOOP',
    'components' => [
        'cache' => [
            'class' => 'yii\caching\FileCache',
        ],
        'authManager' => [
                'class' => 'yii\rbac\DbManager',
        ],
        'formatter' => [
                'class' => 'yii\i18n\Formatter',
                'dateFormat' => 'php:m/d/Y',
                'datetimeFormat' => 'php:m/d/Y H:i:s',
                'timeFormat' => 'php:H:i:s',
        ],              
                'frontendUrlManager'=>[
           'class' => 'yii\web\urlManager',
           'enablePrettyUrl'=>true,
           'showScriptName'=>false,
           'baseUrl'=>'/fuloop/frontend/web',
       ],
    ],
];

我不熟悉 Yii 框架,所以我不确定我应该在哪里寻找它, 提前感谢您的帮助。

【问题讨论】:

    标签: php apache api yii


    【解决方案1】:

    您真的在这条路径上找到您的项目吗?

    <Directory "/Applications/XAMPP/xamppfiles/htdocs">
    

    因为这是我的本地 XAMP 配置。

    你可以看到参数Directory使用路径到web目录到项目目录

    <VirtualHost *:80> 
     ServerName app-rest.dev
     DocumentRoot "/Applications/XAMPP/xamppfiles/htdocs/app-proj/web/"  
    <Directory "/Applications/XAMPP/xamppfiles/htdocs/app-proj/web/" > 
      Options Indexes FollowSymLinks Includes execCGI 
      AllowOverride All 
      Allow From All 
      Order Allow,Deny 
      RewriteEngine on
     </Directory> 
    </VirtualHost>
    

    此外,您还可以在 guide 中找到有用的信息

    例如在我的 config/web.php 中,我将此规则用于 api 控制器

     'urlManager' => [
            'enablePrettyUrl' => true,
            'showScriptName' => false,
            'rules' => [
                ['class' => 'yii\rest\UrlRule', 
                 'pluralize'=>false,
                 'controller' => 'v1/app',  
                    'patterns'   => [
                        'OPTIONS <action>' => 'options', 
                    ],
                 ],
             ],
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-28
      • 2016-07-22
      • 1970-01-01
      相关资源
      最近更新 更多