PHPackages                             offline/laravel-local-cache - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Caching](/categories/caching)
4. /
5. offline/laravel-local-cache

AbandonedArchivedLibrary[Caching](/categories/caching)

offline/laravel-local-cache
===========================

Cache remote files locally in Laravel

v1.0.8(10y ago)43142MITPHPPHP &gt;=5.4.0

Since Jun 16Pushed 7y ago1 watchersCompare

[ Source](https://github.com/OFFLINE-GmbH/laravel-local-cache)[ Packagist](https://packagist.org/packages/offline/laravel-local-cache)[ RSS](/packages/offline-laravel-local-cache/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (9)Dependencies (3)Versions (10)Used By (0)

Local File Cache for Laravel 5
==============================

[](#local-file-cache-for-laravel-5)

\[[![Build Status](https://camo.githubusercontent.com/a4a4df41a0be1f57bfa1a38bee41c12dd89895ac89f49c199863230e9b82b82e/68747470733a2f2f7472617669732d63692e6f72672f4f46464c494e452d476d62482f6c61726176656c2d6c6f63616c2d63616368652e737667)](https://camo.githubusercontent.com/a4a4df41a0be1f57bfa1a38bee41c12dd89895ac89f49c199863230e9b82b82e/68747470733a2f2f7472617669732d63692e6f72672f4f46464c494e452d476d62482f6c61726176656c2d6c6f63616c2d63616368652e737667)\](.org/OFFLINE-GmbH/laravel-local-cache)

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
----------

[](#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
------

[](#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.

```
$string = 'http://www.offlinegmbh.ch/file.jpg';

// returns http://yoursite/cache/{hash}
var_dump(LocalCache::getCachedHtml($string));

```

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.

```
$string = 'http://www.offlinegmbh.ch/file.jpghttp://www.offlinegmbh.ch/file2.jpg';

// http://yoursite/cache/{hash1}http://yoursite/cache/{hash2}
var_dump(LocalCache::getCachedHtml($string));
```

To prevent URLs from being cached, prefix them with a `@` symbol:

```
$string = '@http://dont-cache-me/file.jpg';
// http://dont-cache-me/file.jpg
var_dump(LocalCache::getCachedHtml($string));
```

### Example Middleware

[](#example-middleware)

This example middleware caches all external files referenced in your template and replaces the URLs.

```
