Yii2 RestFull API-生产时出现错误404,仅执行少量操作

问题描述 投票:0回答:1

我有问题,我使用Yii2为API创建了许多动作。我遇到了一个最近的问题:虽然一切都可以在localhost上运行,但是当我在服务器上上传时,以下操作将返回404错误。

我一直在尝试许多不同的解决方案(尝试创建另一个控制器),但均未成功

这些功能的目标是上传/删除图像。

config / main.php

      'urlManager' => [
            'enablePrettyUrl' => true,
            'showScriptName' => false,
            'rules' => require 'urls.php',
        ],

config / urls.php

    'POST api/controller/<id>/logo-upload' => 'controller-name/logo-upload',
    'POST api/controller/<id>/background-image-upload' => 'controller-name/background-image-upload',
    'POST api/controller/<id>/gallery-upload' => 'controller-name/update-gallery',
    'POST api/controller/<id>/delete-logo' => 'controller-name/delete-logo-image',
    'POST api/controller/<id>/delete-background' => 'controller-name/delete-background-image',
    'POST api/controller/<id>/gallery/<galleryID>/delete' => 'controller-name/delete-gallery-image',

这里是我创建的示例控制器。我更改了一些变量名或类名,但没有改变逻辑。

controller-name.php

    <?php


namespace frontend\controllers;

use yii\rest\ActiveController;
use yii\filters\Cors;
use yii\helpers\ArrayHelper;
use yii\filters\ContentNegotiator;
use yii\web\Response;
use yii\helpers\BaseJson;
use yii\data\ActiveDataProvider;
use yii\web\UploadedFile;
use Yii;


class Controller extends RestController
{


    public $modelClass = 'common\models\Model';

    public function actions()
    {
        $actions = parent::actions();

        unset($actions['delete'], $actions['create'], $actions['update'], $actions['index'], $actions['options']);

        return $actions;
    }

    public function behaviors()
    {

        return ArrayHelper::merge([
            [
                'class' => Cors::className(),
                'cors' => [
                    'Origin' => ['*'],
                    'Access-Control-Request-Method' => ['GET', 'HEAD', 'OPTIONS', 'POST'],
                ],
            ],
            [
                'class' => 'yii\filters\ContentNegotiator',
                'only' => ['view', 'index', 'update'],  // in a controller
                // if in a module, use the following IDs for user actions
                // 'only' => ['user/view', 'user/index']
                'formats' => [
                    'application/json' => Response::FORMAT_JSON,
                ],
                'languages' => [
                    'en',
                    'fr',
                ],
            ]
        ], parent::behaviors());
    }



    protected function verbs()
    {
        return [
            'index' => ['GET', 'HEAD'],
            'view' => ['GET', 'HEAD'],
            'create' => ['POST'],
            'update' => ['POST', 'PUT', 'PATCH'],
            'delete' => ['DELETE'],
        ];
    }

    protected function findModel($id)
    {
        if (($model = Model::findOne($id)) !== null &&
            (Yii::$app->user->identity->isAdmin() || $model->owner_id === Yii::$app->user->id)
        ) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
    public function actionLogoUpload($id)
    {
      $card = $this->findModel($id);
      $image = Image::upload($card, 'logoImageFile');
      if ($image->errors) {
        return $result = ["success"=>false, "message"=> $image->getErrors()];
      }
      if (!empty($image)) {

          $card->image_id = $image->id;
          if ($card->validate() && $card->save()) {
            $result = [
                "success"
            ];
            // }
            return $result;
          } else {
            return $result = ["success"=>false, "message"=> $card->getErrors()];
          }
      }

    }

}

php api yii2 yii2-advanced-app
1个回答
0
投票

该问题可能与您的服务器配置有关。您必须配置服务器以将未知路径重写为index.php脚本。

例如,当我将yii2与apache一起使用时,我具有以下web/.htaccess文件:

# 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

# ...other settings...
Options +FollowSymLinks

当然,许多细节取决于您的托管和服务器配置(例如,允许.htaccess文件)。 Yii的文档提供了有关配置不同Web服务器here的更多信息。

© www.soinside.com 2019 - 2024. All rights reserved.