Extra FlashComponent automatic feature request

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!

Why are you forcing the flash to render with the notify element, instead of letting it do it’s default thing of using e.g. a success element if you called $this->Flash->success(…), error element if you called $this->Flash->error(…), etc.? If you want it all to flow through one element for code re-use, you might make those other elements just call the notify element with the extra parameter added there?

Fair point.

Ill try to answer your question with a question to explain my situation.
Why have 4 different elements for the flash messages which are code wise all the same except for the html class “error”, “success”, “info” etc?

I’m using a backend a frontend and a PWA in the same framework that all have their own styled and different flash messages. So that’s 12 elements just to handle some flash messages like success and error with just one word difference (the html class) . So why not make this dynamic? If you have just the single backend i’m totaly fine with this method, beter yet i have never even needed to change this. But i have 3 applications in one and i started to notice the “bloat” or “bulk” of it.

It feels so wasteful not to be able to use one unique element (or 3 in my case) with a dynamic class name but now i have to have allot of almost identical files if i do it the official way.

It feels like so much overhead, sure if you only have a single application with four elements it’s ok but i have a site with a backend, frontend and PWA in one. And i just hate the idea of having so many files doing almost the same thing just with one word that is different that’s why i’m kinda suggesting this addition.

You could stay on using

<?= $this->Flash->render() ?>

in your layout and then use your 1 element in each of your element/flash/something.php files. If they are actually all the same, then you can also just create symlinks as well.


Also maybe GitHub - dereuromark/cakephp-flash: A CakePHP Flash plugin for more powerful flash messages has a bit more features you desire, so maybe take a look at that.


Otherwise feel free to create your own Helper & Component combo where you set data in the request/session in your component and read it via you helper and output it in the template.