Download the PHP package offline/laravel-local-cache without Composer
On this page you can find all versions of the php package offline/laravel-local-cache. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download offline/laravel-local-cache
More information about offline/laravel-local-cache
Files in offline/laravel-local-cache
Package laravel-local-cache
Short Description Cache remote files locally in Laravel
License MIT
Informations about the package laravel-local-cache
Local File Cache for Laravel 5
This package allows you to cache remote files in the local filesystem. This is useful if you try to reduce bandwidth consumption when requesting files from services like Amazon S3.
The implementation also enables you to serve files from cache that may not be available at their remote location at times.
Install it
To install this package include it in your composer.json
and run composer update
:
"require": {
"offline/laravel-local-cache": "~1.0"
}
Add the Service Provider to the provider
array in your config/app.php
'Offline\LocalCache\LocalCacheServiceProvider'
Add an alias for the facade to your config/app.php
'LocalCache' => 'Offline\LocalCache\Facades\LocalCache',
Publish the config:
$ php artisan vendor:publish --provider="Offline\LocalCache\LocalCacheServiceProvider"
Create the directory storage/localcache
(edit the storage_path
setting in config/localcache.php
to change
this location).
Use it
To cache a file use the getCachedHtml
method. The file will be downloaded and stored to disk.
The method returns the local URL for your file.
By default, a /cache/{hash}
route is generated which serves the file's contents with the correct mime type.
To change the route, edit the route
setting in config/localcache.php
.
The getCachedHtml
method works with any string that contains any number of URLs. It extracts and replaces the links
accordingly.
To prevent URLs from being cached, prefix them with a @
symbol:
Example Middleware
This example middleware caches all external files referenced in your template and replaces the URLs.
<?php
namespace App\Http\Middleware;
use Closure;
use Offline\LocalCache\Facades\LocalCache;
class LocalCacheMiddleware
{
/**
* Cache and replace links to external assets.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
*
* @return mixed
*/
public function handle($request, Closure $next)
{
$response = $next($request);
$response->setContent(
LocalCache::getCachedHtml($response->getContent())
);
return $response;
}
}
Add it to your app/Http/Kernel.php
/**
* The application's route middleware.
*
* @var array
*/
protected $routeMiddleware = [
'localcache' => \App\Http\Middleware\LocalCacheMiddleware::class,
];
And use it in your routes.php
or controller.
Route::get('/', [
'uses' => 'PageController@index',
'middleware' => ['localcache']
]);