How to set flash message in sonata admin Admin Controller
Yes, you can set a flash message in an admin class. First, you can define a custom flash message type for the SonataCoreBundle. For example, if you want a success flash message type, add this in the app/config/config.yml
file:
sonata_core: flashmessage: success: types: - { type: mytodo_success, domain: MyToDoBundle }
Then, you need to know when to set the message. For example, if you want to set the message after creating a new entity, you can do so overriding the postPersist
function in your admin class, and adding the message in the Symfony flash bag:
public function postPersist($object) { $this->getRequest()->getSession()->getFlashBag()->add("mytodo_success", "My To-Do custom success message");}
This way, the message will be displayed whenever you create a new entity at the admin class.
You can also use the default type success:
public function postPersist($object) { $this->getRequest()->getSession()->getFlashBag()->add("success", "My To-Do custom success message");}
Because it is an Admin class I get the flashbag over the Session service:
protected function whereever(){ $this->getFlashBag()->add( 'info', 'Your message' );}...protected function getFlashBag(){ return $this->getConfigurationPool()->getContainer()->get('session')->getFlashBag();}
Cheers
You are talking about an admin class, not a controller.
And this is not possible by default. Best way to do this is to write a custom CRUDController (extend from default one) and handle it there.