代码之家  ›  专栏  ›  技术社区  ›  Fadly Dzil

VirtualHost Apache2访问Yii2 Basic中的独立Restful API时失败

  •  -1
  • Fadly Dzil  · 技术社区  · 7 年前

    基本上我遵循这个教程 Diggin 用于在yii2basic中创建restfull api。

    所以我创建了这个目录模式,

    + api
       + config
         - api.php
         - params.php
       + modules
         + v1
             + controllers
               - UserController
         Module.php
    + config
       - web.php
       - params.php
    

    <?php
    
    namespace app\api\modules\v1\controllers;
    
    use yii\filters\VerbFilter;
    use yii\rest\ActiveController;
    
    
    class UserController extends ActiveController
    {
        public $modelClass = 'app\models\User';
    
        public function behaviors()
        {
            return [
                'verbs' => [
                    'class' => VerbFilter::className(),
                    'actions' => [
                        'index' => ['get'],
                        'view' => ['get'],
                        'create' => ['post'],
                        'update' => ['post'],
                        'delete' => ['delete'],
                        'deleteall' => ['post'],
                    ],
    
                ]
            ];
        }
    }   
    

    这是你的名字api.php文件

    <?php
    
    $db = require(__DIR__ . '/../../config/db.php');
    $params = require(__DIR__ . '/params.php');
    
    $config = [
        'id' => 'basic',
        'name' => 'RESTFull',
        'basePath' => dirname(__DIR__) . '/..',
        'bootstrap' => ['log'],
        'components' => [
            'request' => [
                'parsers' => [
                    'application/json' => 'yii\web\JsonParser',
                ]
            ],
            'user' => [
                'identityClass' => 'app\models\User',
                'enableAutoLogin' => false,
            ],
            'log' => [
                'traceLevel' => YII_DEBUG ? 3 : 0,
                'targets' => [
                    [
                        'class' => 'yii\log\FileTarget',
                        'levels' => ['error', 'warning'],
                        'logFile' => '@app/runtime/logs/api.log',
                    ],
                ],
            ],
            'urlManager' => [
                'class' => 'yii\web\UrlManager',
                'showScriptName' => false,
                'enablePrettyUrl' => true,
                'enableStrictParsing' => true,
                'rules' => [
                    [
                        'class' => 'yii\rest\UrlRule',
                        'controller' => ['v1/user']
                    ],
                    '<controller:\w+>/<id:\d+>' => '<controller>/view',
                    '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
                    '<controller:\w+>/<action:\w+>' => '<controller>/<action>',
                ],
            ],
            'db' => $db,
        ],
        'modules' => [
            'v1' => [
                'basePath' => '@app/api/modules/v1',
                'class' => 'app\api\modules\v1\Module' // here is our v1 modules
            ],
        ],
        'params' => $params,
    ];
    
    return $config;
    

    这是.htaccess

    Options +FollowSymLinks
    IndexIgnore */*
    
    RewriteEngine on
    
    # if a directory or a file exists, use it directly
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    
    # otherwise forward it to index.php
    RewriteRule . index.php
    

    我尝试使用此url在postman中获取数据: http://localhost/app_folder/api/v1/user ,感谢上帝,我得到了正确的Json数据。

    [
        {
            "id": 1,
            "username": "Administrator",
            "email": "email-me.com",
            "auth_key": "just-key",
            "password_hash": "just_password",
            "password_reset_token": null,
            "status": 10,
            "created_at": 1476816334,
            "updated_at": 1487220512
        },
    ]
    

    但是当我尝试使用Virtualhost时

    <VirtualHost *:2200>
    ServerName myserver.local   
    DocumentRoot d:/wamp64/www/app_folder/api
    <Directory  "d:/wamp64/www/app_folder/api/">
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Require all granted
    </Directory>
    

    也就是说,我在windows计算机上,所以我必须在C:\windows\System32\drivers\etc>记事本主机中添加一个新名称

    127.0.0.1   myserver.local
    ::1 myserver.local
    

    http://myserver.local:2200/api/v1/user

    Could not get any response
    There was an error connecting to http://myserver.local:2200/api/v1/user
    

    我错过了什么,请告知

    1 回复  |  直到 7 年前
        1
  •  1
  •   Mihai P.    7 年前

    您已经将虚拟主机指向api目录。万一你有索引.php把你的请求归档

    http://myserver.local:2200/v1/user

    http://myserver.local:2200/api/v1/user