如何在删除非空目录之前闪烁错误消息


How to flash a error message before deleting a non empty directory

我想删除与记录关联的目录,但如果目录不为空,则不删除,尽管我知道默认情况下我们无法删除非空目录rmdir()并且它会闪烁错误。但这是一个编译器错误,我想在应用程序中打印一个错误,告诉用户为什么无法删除目录。基本上我正在寻找的是这样的东西:-

 public function actionDelete($id)
 if(some condition here to check the dir is empty)
 {
        rmdir("path of the dir")
 }else {
       a flash msg here saying directory is not empty 
 }

前提是使用yii2 PHP 框架。

您可以使用 setFlash() 方法处理 Flash 消息。

例如

public function actionDelete($id)
{
  try
  {
     rmdir("path of the dir")
  }
  catch(Exception $e) 
  {
     Yii::$app->session->setFlash('error', 'Custom error message or catched exception.')); 
  }
}