Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to set custom cache paths in middleware to support multiple domains #103

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ This package allows you to easily cache responses as static files on disk for li
- [Using the middleware](#using-the-middleware)
- [Clearing the cache](#clearing-the-cache)
- [Customizing what to cache](#customizing-what-to-cache)
- [Multiple domains](#multiple-domains)
- [License](#license)

---
Expand Down Expand Up @@ -210,6 +211,21 @@ By default, all GET requests with a 200 HTTP response code are cached. If you wa

3. Finally, update the middleware references in your `app/Http/Kernel.php` file, to point to your own middleware.

### Multiple Domains

Out of the box, Page Cache stores the cached files in a single directory based off the file path. When using caching on a Laravel application that has mutiple domains these files will conflict with each other. The `page-cache` middleware supports an argument that allows you to configure the directory of where to store the cached file. You can store these in a subdirectory (`page-cache/laravel`) or store them in different directories (`page-cache-laravel`).

```php
Route::domain('laravel.com')->('page-cache:page-cache/laravel')->get('/', 'LaravelController@show')->name('home');
Route::domain('forge.laravel.com')->('page-cache:page-cache/forge')->get('/', 'ForgeController@show');
```

When using directories outside of the default `page-cache` directory, you will need to set the cache path prior to forgetting the files, and the artisan command with no longer work (until it is updated to support custom cache paths).

```php
tap(app()->make(Cache::class))->setCachePath('page-cache-laravel')->forget(route('home', [], false));
```

## License

The Page Cache package is open-sourced software licensed under the [MIT license](https://opensource.org/licenses/MIT).
Expand Down
4 changes: 3 additions & 1 deletion src/Middleware/CacheResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,15 @@ public function __construct(Cache $cache)
*
* @param \Symfony\Component\HttpFoundation\Request $request
* @param \Closure $next
* @param string|null $path
* @return mixed
*/
public function handle(Request $request, Closure $next)
public function handle(Request $request, Closure $next, $path = null)
{
$response = $next($request);

if ($this->shouldCache($request, $response)) {
$this->cache->setCachePath($path);
$this->cache->cache($request, $response);
}

Expand Down