A
A
Alexander Bulatov2019-01-04 17:20:02
Yii
Alexander Bulatov, 2019-01-04 17:20:02

Throws a 404 error in the backend, but not in the frontend. How to fix?

Good day!
Situation:
On the Yii2 Advanced template, the photo is not deleted in the backend. A 404 error is thrown.
Data:
Photos are rendered via Kartik Fileinput Bootstrap.
And it is through the delete button in this widget that a 404 error appears, but the message is that there is no such address.
And the irony is that on a working hosting (not a locale) an error 403 Forbidden is issued, not 404!
At the same time, this error does not exist on the frontend and the photo is deleted, everything is as it should be.
Here is the code in view

$form->field($uploadForm, 'imageFiles[]')->widget(FileInput::class, [
  //'name' => 'attachment_53',
  'options' => ['multiple' => true, 'accept' => 'image/*'],
  'pluginOptions' => [
    'deleteUrl' => Url::to(['/ads-moderation/delete-ad-image']),
    'initialPreviewConfig' => $model->imageLinkData, // конфигурация для удаления фотографий
    //'deleteExtraData' => ,
    'initialPreview' => $model->imageLinksFromBackend ? $model->imageLinksFromBackend : '',
    'initialPreviewAsData' => true,
    'overwriteInitial' => false,
    'fileActionSettings' => [
              //'initialPreviewShowDelete' => false,
              'showZoom' => false,
    ],
    'showCaption' => false,
    'showRemove' => false,
    'showUpload' => false,
    'browseClass' => 'btn btn-primary btn-block',
    'browseIcon' => '<i class="glyphicon glyphicon-camera"></i> ',
    'browseLabel' => Yii::t('kupdam', 'Выбрать фото'),
    'maxFileSize' => 15000,
    'macFileCount' => 10,            
  ],
])

Code in controller (action referenced by url in deleteUrl key)
public function actions() 
    {
        return [
            'list-subcategories' => [
                'class' => 'common\actions\ads\ListSubcategories',
            ],
            'list-regions' => [
                'class' => 'common\actions\ads\ListRegions',
            ],
            'list-cities' => [
                'class' => 'common\actions\ads\ListCities',
            ],
            'delete-ad-image-from-ads-table' => [
                'class' => 'common\actions\ads\DeleteAdImageFromAdsTable',
            ],
            'delete-ad-image' => [
                'class' => 'common\actions\ads\DeleteAdImage',
            ],
        ];
    }

And actually the entire class of the entire action itself
namespace common\actions\ads;

use Yii;
use common\models\AdsPhotos;
use yii\web\NotFoundHttpException;

class DeleteAdImage extends \yii\base\Action
{
    public function run()
    {
        $id = Yii::$app->request->post('key');
        $image = AdsPhotos::find()->where(['photo_id' => $id])->limit(1)->one();
        
        if ($image) {
            $path = Yii::getAlias('@webrootFrontend').$image->path;
            if (file_exists($path)) {
                unlink($path);
                return $image->delete() ?: null;
            } else {
                throw new NotFoundHttpException('A file doesn\'t exixt');
            }
             
        } else {
            throw new NotFoundHttpException('A record doesn\'t exixt');
        }
    }
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question