在Yii中找不到API路由404

时间:2017-01-19 19:42:28

标签: php apache api yii

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

目前该项目正在为后端和前端网址提供服务,但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>

该应用程序遵循&#34; Yii 2高级项目模板&#34;结构,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,
];

一个常见的/ 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框架所以我不确定我应该在哪里寻找这个, 提前感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

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

<option>

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

您可以看到目录的哪个参数使用 web 目录的路径到项目目录

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

此外,您可以在此guide

中找到有用的信息

E.g。在我的config / web.php中,我对api控制器使用此规则

<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>