Cache js files in laravel
<?php
// Add this to your routes.php
Route::get('/js/{file}', 'CacheJsController@cache_js');
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
class CacheJsController extends Controller
{
function __construct()
{
}
public function cache_js(Request $request, $file)
{
$_expires = 20160;
switch ($file) {
case 'analytics.js':
$_remote_file = 'https://www.google-analytics.com/analytics.js';
break;
default:
\App::abort(404);
break;
}
if (!Cache::has($file)) {
$_remote = file_get_contents($_remote_file);
$_time = new \DateTime('now');
Cache::put($file, [$_time, $_remote], $_expires);
} else {
$_cache = Cache::get($file);
$_remote = $_cache[1];
$_time = $_cache[0];
}
$response = Response::make($_remote, 200)
->header('Content-Type', 'text/javascript')
->header('Cache-Control', 'private, max-age=' . $_expires)
->setLastModified($_time)
->setExpires(\Carbon\Carbon::now()->addMinutes($_expires));
return $response;
}
}