Hello CakePHP community,
I will try to sketch my annoying but small and legitimate issue and how i fixed it for myself. The best way i can.
I’ve been using the following codes allot (Cakephp5.2):
$this->Flash->error('Some error text');
$this->Flash->success('Some success text');
And all the other variants that come with it.
I have a lot of these flash messages. And i used to change them all by doing the following.
$this->Flash->error('Some error text', ['params' => ['class' => 'error']]);
$this->Flash->success('Some success text', ['params' => ['class' => 'success']]);
Which in my opinion is really ugly since you already use the ERROR or SUCCESS function from the flashComponent so why define the class again why not pass it along automatically?!
In my layout i have the following.
<?=$this->Flash->render('flash', ['element' => 'notify'])?>
So all the flash message use the notify element. But now comes the thing. The only way you can differentiate between the error and success function is if you add all these overhead array parameters in every single flash function. And i just need the class to trigger the good looking CSS code. And some animations that come with it, i know there are different elements for this but i need a single one in this situation. Something to do with JavaScript and CSS and not having all the double code in all the elements etc etc.
In my situation there are more than 50 flash messages. So in order to make an easy fix i copied the original FlashComponent threw it in my own Component folder and added the following lines of code in the __call function.
if(!isset($args[1]['params']['class'])){
$args[1]['params']['class'] = $name;
}else{
$args[1]['params']['class'] .= ' '.$name;
}
This way the function name is always passed as the CSS class. This can be error, success or info based on which Flash function you call. This saves allot of code and makes it more functional for the front end to work with.
I know this is not the perfect final solution for the framework itself since it has way different standards. For me this works great and saves me allot of overhead code, time and adds extra functionality to the framework itself. But i hope this will trigger someone into thinking that this is a nice feature to implement in some way into the current framework.
I hope i explained it correctly but you can always ask for more info if needed.
Cheers
P.S. Love the framework been using it since version 2!