How to throw an event after delivering the response to the user in laravel?

0

Well basically what I need is to delete some files from the storage folder, but I want that to happen every time a request is terminated.

I have already created an event and a listener that is responsible for doing that, which is called before doing the return in the controller. But since some exception may happen, obviously the event will not be launched.

I need that to always run regardless of whether an exception occurs or not.

The laravel documentation link talks about it but it's in version 4.2 and that's it the current version does not have it.

    
asked by FuriosoJack 04.01.2018 в 17:29
source

1 answer

0

You do not give much detail of how your code is but basically what you have to do is place your event at the end before generating the response and within your try / catch. That way, if the exception is not generated, the event will be handled.

public function SomeAction(Request $reques) {

    try {
        //Operaciones
       event(new CustomEvent($request));
       // return response
    } catch (\Exception $e) {
        // manejas la excepcion
    }

} 
    
answered by 30.01.2018 в 05:50