livewire icon indicating copy to clipboard operation
livewire copied to clipboard

Add `#[Persistent]` attribute for middleware

Open ralphjsmit opened this issue 9 months ago • 1 comments

Hello, thank you very much for all the great work you did and are doing on Livewire!

Quite often it happens that you add a new middleware to your routes, but don't immediately realize that it also needs to be marked as persistent. In my opinion, it is not super-convenient to create the middleware and then look up a (big) service provider or something and then add the middleware in Livewire::addPersistentMiddleware().

This PR proposes a very easy solution to allow marking a middleware as #[Persistent] with a simple attribute, meaning that developers can just easily mark a middleware as persistent when creating them, without needing to open up a whole other file. It was actually pretty easy to do just inside the filterMiddlewareByPersistentMiddleware(), so I think this would be a great DX-improvement and would result in less middleware being "forgotten" to be marked as persistent.

I also added tests for the method. Thanks!

ralphjsmit avatar Mar 05 '25 18:03 ralphjsmit

This actually is a pretty useful iteration! Think of Multi-tenancy where you have a middleware which resolves tenant based upon subdomain.

maurocasas avatar May 08 '25 03:05 maurocasas