Simple Cache:forget technique for Cached Routes
Oct 27, 2013  

Cached routes are great, but I'm inpatient when it comes to waiting. I've come up with a few different approaches for viewing my changes to blog post like my viewDraft action. However, I like to have options. What I came up with might be useful for someone else, so here's a simplified version of what I threw together.

First, we need to figure out how our cache key is formed in filters.php or <code>routes.php so we can replicate it in our forget action.

Filters.php (Refer here for the full source)

$key = 'route-'.Str::slug(Request::url()).$page;

The $page variable will not come into play here as it's not used on individual posts and will always return empty. So now, let's create the controller action to forget our cached posts routes.

Posts Controller

public function loseCache($id)
    $post = Post::find($id);
    $key = Str::slug(action('HomeController@viewPost', array($post->category->slug, $post->slug)));
    return Redirect::action('PostsController@index');

For testing purposes, you might want to use something like dd($key); to make sure you're recreating the key as needed, but that's my simple technique for getting the job done.

Drop Cache Feature