Today, we describe success and error messages in Magento 2.
messageManager
Display success and error messages with the help of the messageManager object .
protected messageManager;
public function __construct(
---------
\Magento\Framework\Message\ManagerInterface $messageManager,
---------
){
$this->messageManager = $messageManager;
}
public function myFucntion(){
$message =$this->messageManager->addError(__("Error message"));
$message =$this->messageManager->addWarning(__("Warning message"));
$message =$this->messageManager->addNotice(__("Notice message"));
$message =$this->messageManager->addSuccess(__("Success message"));
return $message;
}
Observer
Display success and error messages with the help of the observer.
Throw exception from observer
throw new \Magento\Framework\Exception\CouldNotDeleteException(__("Prices have been changed!"));
Related Post : Magento2 Get Category Image