Answer the question
In order to leave comments, you need to log in
How to delete blog posts?
What is the post deletion mechanism in Yii2? In the project, the blog is rendered as a separate module. If you delete a post on a local site in the backend, then debage does not fix errors, you have to close the page through the task manager, because the browser freezes, and posts are deleted only in the database.
Tried to add such method to Blog.Controller - doesn't work.
public function actionDelete($id=NULL)
{
if ($id === NULL)
{
Yii::$app->session->setFlash('PostDeletedError');
Yii::$app->getResponse()->redirect(array('site/index'));
}
$post = Post::find($id);
if ($post === NULL)
{
Yii::$app->session->setFlash('PostDeletedError');
Yii::$app->getResponse()->redirect(array('site/index'));
}
$post->delete();
Yii::$app->session->setFlash('PostDeleted');
Yii::$app->getResponse()->redirect(array('site/index'));
}
<?php
namespace illyas\blog\controllers;
use Yii;
use common\models\ImageManager;
use medeyacom\blog\models\BlogSearch;
use yii\web\Controller;
use yii\web\MethodNotAllowedHttpException;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
/**
* BlogController implements the CRUD actions for Blog model.
*/
class BlogController extends Controller
{
/**
* @inheritdoc
*/
public function behaviors()
{
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['POST'],
'delete-image' => ['POST'],
'sort-image' => ['POST'],
],
],
];
}
/**
* Lists all Blog models.
* @return mixed
*/
public function actionIndex()
{
$searchModel = new BlogSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
/**
* Displays a single Blog model.
* @param integer $id
* @return mixed
*/
public function actionView($id)
{
return $this->render('view', [
'model' => $this->findModel($id),
]);
}
/**
* Creates a new Blog model.
* If creation is successful, the browser will be redirected to the 'view' page.
* @return mixed
*/
public function actionCreate()
{
$model = new \illyas\blog\models\Blog();
$model->sort = 50;
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('create', [
'model' => $model,
]);
}
}
/**
* Updates an existing Blog model.
* If update is successful, the browser will be redirected to the 'view' page.
* @param integer $id
* @return mixed
*/
public function actionUpdate($id)
{
$model = $this->findModel($id);
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('update', [
'model' => $model,
]);
}
}
/**
* Deletes an existing Blog model.
* If deletion is successful, the browser will be redirected to the 'index' page.
* @param integer $id
* @return mixed
*/
public function actionDelete($id)
{
$this->findModel($id)->delete();
return $this->redirect(['index']);
}
public function actionDeleteImage()
{
if(($model = ImageManager::findOne(Yii::$app->request->post('key'))) and $model->delete()){
return true;
} else {
throw new NotFoundHttpException('The requested page does not exist.');
}
}
public function actionSortImage($id)
{
if(Yii::$app->request->isAjax){
$post = Yii::$app->request->post('sort');
if($post['oldIndex'] > $post['newIndex']){
$param = ['and',['>=','sort',$post['newIndex']],['<','sort',$post['oldIndex']]];
$counter = 1;
}else{
$param = ['and',['<=','sort',$post['newIndex']],['>','sort',$post['oldIndex']]];
$counter = -1;
}
ImageManager::updateAllCounters(['sort' => $counter], [
'and',['class'=>'blog','item_id'=>$id],$param
]);
ImageManager::updateAll(['sort' => $post['newIndex']], [
'id' => $post['stack'][$post['newIndex']]['key']
]);
return true;
}
throw new MethodNotAllowedHttpException();
}
/**
* Finds the Blog model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
* @param integer $id
* @return Blog the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($id)
{
if (($model = \illyas\blog\models\Blog::find()->with('tags')->andWhere(['id'=>$id])->one()) !== null) {
return $model;
} else {
throw new NotFoundHttpException('The requested page does not exist.');
}
}
}
Answer the question
In order to leave comments, you need to log in
posts are deleted only in the database
public function actionDelete($id)
if ($id === NULL)
{
Yii::$app->session->setFlash('PostDeletedError');
Yii::$app->getResponse()->redirect(array('site/index'));
}
public function actionDelete($id){
if (($post = Post::findOne($id)) and $post->delete())
{
Yii::$app->session->setFlash('PostDeleted');
}else{
Yii::$app->session->setFlash('PostDeleted');
}
return $this->redirect(['site/index']);
}
This code is not working
public function actionDelete($id){
if (($post = Post::findOne($id)) and $post->delete())
{
Yii::$app->session->setFlash('PostDeleted');
}else{
Yii::$app->session->setFlash('PostDeleted');
}
return $this->redirect(['site/index']);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question