samedi 16 juin 2018

Get all flash data from session

I am saving a cache of Laravel responses and its working fine. I am using a middleware that checks if there exists a cache for the request and return the cached response instead of generating that response again.

But there is a problem with flash messages when there is a flash message to be shown to the user it returns the cashed response which doesn't have any flash message.

Therefore I want to check if there is a flash message without knowing te key. So that I can return non-cached response with flash message.

Here is that middleware

public function handle($request, Closure $next, $ttl=1440)
    {
        if(authenticate_user() != null || $request->isMethod('post'))
            return $next($request);
        $params = $request->query(); unset($params['_method']); ksort($params);
        $key = md5(url()->current().'?'.http_build_query($params));
        if($request->get('_method')=='purge')
            Cache::forget($key);
        if(Cache::has($key)){
            $cache = Cache::get($key);
            $response = response($cache['content']);
            $response->header('X-Proxy-Cache', 'HIT');
        }
        else {
            $response = $next($request);
            Cache::put($key,['content' => $response->content(), 'headers' => array_map(function($element){ return implode(',', $element); }, $response->headers->all())],$ttl);
            $response->header('X-Proxy-Cache', 'MISS');
        }

        return $response;
    }

Here is the dd(session()). In case it can help someone

enter image description here

I want the data corresponding to success key.

But I don't want to use a specific key to get flash data. I just want to check if there is a flash message irrespective of the key used to store that data.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire