Proxying images to a remote host on Laravel Valet

In an earlier post, I talked about loading images by proxy using NGINX directives which can be a great technique to avoid having to download media libraries every time you sync a live site to your local machine which saves you both time and disk space.

In another article where I talked about using Laravel Valet for WordPress development, I touched on the ability to proxy images using PHP-based local drivers and thought I should share a bit of code around just that.

Laravel Valet's Local Driver

While getting familiar with Valet, I learned of its built-in driver system for supporting various frameworks and also learned of the WordPressValetDriver built right into the Valet package. These drivers are basically responsible for providing support for the file structure of various frameworks.

Especially interesting to me was the notion of local drivers. Simply put, by adding a LocalValetDriver class with the same filename to the root of your project, you can take control of how Valet serves your project.

Using Local Drivers to Proxy Images From Production

After some experimentation, I found a way to load any images that were missing from the local uploads directory from a remote host – in my case, this is the live site.

Drop the following class into your project's root directory, change out the REMOTE_HOST constant, and make sure you name the file LocalValetDriver.php. This should just work.