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

comancsm's avatar

Laravel Full Page Caching

Hello,

I am trying to implement page caching on certain routes, just like in the lesson here: https://laracasts.com/lessons/caching-essentials.

However, Laravel 5.2 doesn't support filters anymore, and i am forced to use middleware as seen below.

Before Middleware:

  public function handle($request, Closure $next)
    {
        $response = $next($request);
        $cache=new CacheController();
        $key=$cache->make_cache_key($request->fullUrl());

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

        return $response;
    }

After Middleware:

public function terminate($request,Response $response){

        $cache=new CacheController();
        $key=$cache->make_cache_key($request->fullUrl());



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

The caching works in a way. The cached response is returned and the page rendered, but the execution of the following code isn't stopped.

The controller logic placed after the middleware still gets executed, so do the queries, even though the page is already rendered to the user.

Is there a way to stop the request from going forward?

I tried just putting "return Cache::get('$key')" but this just returns an error:

FatalThrowableError in StartSession.php line 172:
Type error: Argument 1 passed to Illuminate\Session\Middleware\StartSession::addCookieToResponse() must be an instance of Symfony\Component\HttpFoundation\Response, string given, called in /home/vagrant/Code/Watch/vendor/laravel/framework/src/Illuminate/Session/Middleware/StartSession.php on line 72

Thanks in advance!

0 likes
4 replies
comancsm's avatar

Thanks for the quick reply @freekmurze.

I found your package when i was looking for information on caching, but i prefer doing it on my own as i will require logic based on cache tags depending on route.

Could you shed some light on what i am doing wrong here?

freekmurze's avatar

Try removing:

$response = $next($request);

And replacing

return $response;

by

return $next($request);

Please or to participate in this conversation.