Generated CRUD operations via Gii, added image upload functionality to the article. After that, for some reason, the removal of articles stopped working.

Here is the removal

public function actionDelete($id) { $this->findModel($id)->delete(); return $this->redirect(['index']); } 

as far as I could understand, findModel () does not work in the actionDelete method (drove everything into if, redirect did not pass). I could not find out the reason. here is findModel

 protected function findModel($id) { if (($model = Article::findOne($id)) !== null) { return $model; } throw new NotFoundHttpException('The requested page does not exist.'); } 

Here are the logs:

 2019-02-06 22:49:04 [127.0.0.1][-][b1h7sqlc3rrjq5on8tokk5iq5g1p629m][error][yii\web\HttpException:404] exception 'yii\base\InvalidRouteException' with message 'Unable to resolve the request "uploads/c8792d9548210f5455f395cdc73386f3.png".' in D:\ospanel\OSPanel\domains\localhost\footblog\vendor\yiisoft\yii2\base\Module.php:537 Stack trace: #0 D:\ospanel\OSPanel\domains\localhost\footblog\vendor\yiisoft\yii2\web\Application.php(103): yii\base\Module->runAction('uploads/c8792d9...', Array) #1 D:\ospanel\OSPanel\domains\localhost\footblog\vendor\yiisoft\yii2\base\Application.php(386): yii\web\Application->handleRequest(Object(yi i\web\Request)) #2 D:\ospanel\OSPanel\domains\localhost\footblog\web\index.php(12): yii\base\Application->run() #3 {main} Next exception 'yii\web\NotFoundHttpException' with message 'Page not found.' in D:\ospanel\OSPanel\domains\localhost\footblog\vendor\yiisoft\yii2\web\Application.php:115 Stack trace: #0 D:\ospanel\OSPanel\domains\localhost\footblog\vendor\yiisoft\yii2\base\Application.php(386): yii\web\Application->handleRequest(Object(yii\web\Request)) #1 D:\ospanel\OSPanel\domains\localhost\footblog\web\index.php(12): yii\base\Application->run() #2 {main} 2019-02-06 22:49:04 [127.0.0.1][-][b1h7sqlc3rrjq5on8tokk5iq5g1p629m][info][application] $_GET = [] $_POST = [] $_FILES = [] $_COOKIE = [ 'PHPSESSID' => 'b1h7sqlc3rrjq5on8tokk5iq5g1p629m' '_csrf' => 'a83c3e44210e8a2e85885e631b6a0ac853ccbcaf0b62a4a21160636938f7ebe8a:2:{i:0;s:5:\"_csrf\";i:1;s:32:\"bUvzAuOr6NyocoDZlb2VFCGF07-HT68H\";}' 

I ask to help with a solution of a problem or to prompt where to look for an error.

upd just in case, add pictures action:

 public function actionAddImage($id) { $model = new ImageUpLoad; if(Yii::$app->request->isPost) { $article = $this->findModel($id); $file = UploadedFile::getInstance($model, 'image'); //$model->uploadFile($file); if($article->saveImage($model->uploadFile($file, $article->image))) { return $this->redirect(['view', 'id'=>$article->id]); } } return $this->render('image', ['model'=>$model]); } 

In the base of the record remain

  • Show full model class - edvardpotter
  • The class $ model = new ImageUpLoad; on actionAddImage. Show the controller completely - DIlshod

1 answer 1

With this code, I think that everything is fine. The problem seems to me in the request itself. Judging by the logs when requesting an image, the yii2 application starts to work, which does not understand how to parse the request for uploads / c8792d9548210f5455f395cdc73386f3.png. This is corrected by the web server settings (nginx or apache, depending on what is used).

But at the expense of deletion, you need to debug or place your exceptions to understand. Perhaps before deleting, a trigger triggers to delete the image (see use of EVENT_BEFORE_DELETE in the model) and when deleting a picture, the rights on the web server side may not be enough and then the event will not work and removal will happen. In general, xdebug to help.