Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

peitje's avatar

Response content Cache

Hello,

I want to cache pages in Laravel 5.1 like this (https://laracasts.com/lessons/caching-essentials (L4)) This is my afterFilter:

class AfterMiddleware
{
    public function handle($request,  Closure $next)
    {
        $key = $this->makeCachKey($request->url());
        if(!Cache::has($key)){
            return Cache::put($key,Response::getContentFunctionOrSomething(),60);
        }
        return $next($request);
    }
    protected function makeCachKey($url){
        return 'route_'.Str::slug($url);
    }
}

In L4 is is Response::getContent, what function it is in L5?

0 likes
12 replies
peitje's avatar

Thank you @sid405 I think you pointed me in the right direction but i can't get in working.

if I do it like this:

class AfterMiddleware
{
    protected $response;
    public function __construct(Response $response)
    {
        $this->response = $response;
    }
    public function handle($request,  Closure $next)
     {
       // return empty?
        dd( $this->response->getOriginalContent());
        $key = $this->makeCachKey($request->url());
      ....
   ...
}

I get a empty string. Maybe it is not called after the request? My route; Route::group(array('prefix' =>null,'middleware'=>['BeforeMiddleware','AfterMiddleware']), function() {

My kernel.php protected $routeMiddleware = [ 'BeforeMiddleware' => 'App\Http\Middleware\BeforeMiddleware', 'AfterMiddleware' => 'App\Http\Middleware\AfterMiddleware', .. Thank your!

sid405's avatar

where is $this->response coming from in your case?

peitje's avatar

Thank you for your assistance,

It was easy actialy, I do it like this:

class AfterMiddleware
{
    public function handle($request,  Closure $next)
     {
        $response = $next($request);//get te response!
    $response =   $response->getContent();
        $key = $this->makeCacheKey($request->url());
        if(!Cache::has($key)){
            Cache::put($key,$response ,60);
        }
        return $response ;
    }
    protected function makeCacheKey($url){
        return 'route_'.Str::slug($url);
    }
}
1 like
sid405's avatar
sid405
Best Answer
Level 27

@peitje Okay got it working and learned a few things on the way. Best way to go.

Here it is.

BeforeMiddleWare.php

<?php 

namespace App\Http\Middleware;

use Closure;
use Cache;
use Illuminate\Support\Str;

class BeforeMiddleWare
{
    /**
 * Handle an incoming request.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  \Closure  $next
 * @return mixed
 */
    public function handle($request, Closure $next)
    {
        $key = $this->keygen($request->url());

        if (Cache::has($key)) {
            $content=Cache::get($key);
            return response($content);
        }

        return $next($request);
    }

    protected function keygen($url)
    {
        return 'route_' . str_slug($url);
    }
}

AfterMiddleware.php

<?php

namespace App\Http\Middleware;

use Closure;
use Cache;
use Illuminate\Support\Str;
use Event;

class AfterMiddleWare
{
    /**
 * Handle an incoming request.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  \Closure  $next
 * @return mixed
 */
    public function handle($request, Closure $next)
    {
        $response = $next($request);

        $key = $this->keygen($request->url());

        if (! Cache::has($key)) {
            Cache::put($key, $response->getContent(), 1);
        }

        return $next($request);
    }

    protected function keygen($url)
    {
        return 'route_' . str_slug($url);
    }
}

Kernel.php

protected $routeMiddleware = [
        ...

        'cacheafter' => \App\Http\Middleware\AfterMiddleware::class,
        'cachebefore' => \App\Http\Middleware\BeforeMiddleware::class,
    
    ...
    ];

It's working just fine.

ps. I had to make something like this for a project i start tomorrow. I guess it started a day early :P

3 likes
peitje's avatar

Very nice, we found the same solution! you can return the $resonse in the AfterMiddleWare.

My site is a lot faster now! I use Cache::flush(); when I want to edit the contents

Good luck with your project today and tomorrow!

sid405's avatar

@peitje Excellent!

Yep i'm making a quick package for private consumption of this. I know i'll be using it a bunch

Good luck

ps. Don't forget to vote :p

1 like
peitje's avatar

NICE! I will use this package very soon. My site is more than twice as fast now :)

kyslik's avatar

@sid405 Shouldn't afterMiddleware handle method return (already processed $response)?

...
return $response

Please or to participate in this conversation.