【问题标题】:Yii2 Advanced Template - Rest APIYii2 高级模板 - Rest API
【发布时间】:2016-05-21 13:10:19
【问题描述】:

我一直在关注一些在线教程,但我似乎无法弄清楚如何为 Yii2 创建一个 REST API。我不断收到404 page not found。这是我目前所拥有的:

文件夹结构:

api
- config
-- api.php
-- bootstrap.php
- modules
-- v1
--- controllers
---- UserController.php
--- Api.php
--- Module.php
.htaccess
index.php

api/api.php

<?php

$params = require(__DIR__ . '/params.php');

$config = [
    'id' => 'api',
    'basePath'  => dirname(__DIR__).'/..',
    'bootstrap'  => ['log'],

    'components'  => [
        // URL Configuration for our API
        'urlManager'  => [
            'enablePrettyUrl'  => true,
            'showScriptName'  => false,

            'rules' => [
                [
                    'class'  => 'yii\rest\UrlRule',
                    'controller'  => [
                        //'v1/user'
                        'users' => 'v1/users'
                    ],
                ]
            ],

        ],

        'request' => [
            // Set Parser to JsonParser to accept Json in request
            'parsers' => [
                'application/json'  => 'yii\web\JsonParser',
            ]
        ],

        'cache'  => [
            'class'  => 'yii\caching\FileCache',
        ],

        // Set this enable authentication in our API
        'user' => [
            'identityClass'  => 'app\models\User',
            'enableAutoLogin'  => false, // Don't forget to set Auto login to false
        ],

        // Enable logging for API in a api Directory different than web directory
        'log' => [
            'traceLevel'  => YII_DEBUG ? 3 : 0,
            'targets'  => [
                [
                    'class'  => 'yii\log\FileTarget',
                    'levels'  => ['error', 'warning'],
                    // maintain api logs in api directory
                    'logFile'  => '@api/runtime/logs/error.log'
                ],
            ],
        ],

        'db'  => require(__DIR__ . '/../../config/db.php'),
    ],

    'modules' => [
        'v1' => [
            'basePath' => '@api/modules/v1',
        'class' => 'api\modules\v1\Api',
        ]
    ],

    'params'  => $params,
];

return $config;

api/bootstrap.php

<?php
    Yii::setAlias('api', dirname(dirname(__DIR__)) . '/api');

api/modules/v1/controllers/UserController.php

<?php

namespace app\api\modules\v1\controllers;

class UserController extends ActiveController
{

    public $modelClass = 'app\models\User';
}

api/modules/v1/Api.php

<?php

namespace api\modules\v1;

use \yii\base\Module;

class Api extends Module
{
    public $controllerNamespace = 'api\modules\v1\controllers';

    public function init()
    {
        parent::init();

        // custom initialization code goes here
    }
}

api/modules/v1/Module.php

<?php

namespace api\modules\v1;

class Module extends \yii\base\Module
{
    public $controllerNamespace = 'api\modules\v1\controllers';

    public function init()
    {
        parent::init();
        \Yii::$app->user->enableSession = false;
        // custom initialization code goes here
    }
}

api/.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

api/index.php

<?php

defined('YII_DEBUG') or define('YII_DEBUG', true);
defined('YII_ENV') or define('YII_ENV', 'dev');

require(__DIR__ . '/../vendor/autoload.php');
require(__DIR__ . '/../vendor/yiisoft/yii2/Yii.php');

require(__DIR__ . '/config/bootstrap.php');

$config = require(__DIR__ . '/config/api.php');

$application = new yii\web\Application($config);

$application->run();

mod_rewrite 已启用

不确定这里还需要做什么。我也将此添加到common/config/main.php

'modules' => [

    'user' => [
        'class' => 'dektrium\user\Module',            
    ],

    'v1' => [
        'basePath' => '@api/modules/v1',
        'class' => 'api\modules\v1\Api',
    ]

],

我使用 Postman 发送 GET 请求: http://example.com/api/v1/users

http://example.com/v1/users 返回Class app\api\modules\v1\Api does not exist

我对配置和文件做了一些更改,但没有成功。这是我拥有的最干净的版本。

【问题讨论】:

  • 'class' =&gt; 'app\api\modules\v1\Api',Api.phpModule.php 中的命名空间不匹配。
  • 已更改,但在使用http://example.com/api/v1/users 时仍会得到404 not found。问题也发生了变化。

标签: php .htaccess api rest yii2-advanced-app


【解决方案1】:

请试试这个

您的控制器名称是UserController 并且您正在访问api中的用户

尝试访问api url中的用户

【讨论】:

    猜你喜欢
    • 2017-04-12
    • 1970-01-01
    • 1970-01-01
    • 2016-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-28
    • 1970-01-01
    相关资源
    最近更新 更多