Skip to content

Instantly share code, notes, and snippets.

@milesw
Created December 3, 2016 08:02
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save milesw/832ffe73c1e9ff3af81f5148f8e0ee45 to your computer and use it in GitHub Desktop.
Save milesw/832ffe73c1e9ff3af81f5148f8e0ee45 to your computer and use it in GitHub Desktop.
Laravel Valet driver that serves static files with CORS header
<?php
class StaticCorsValetDriver extends BasicValetDriver
{
/**
* Determine if the driver serves the request.
*
* @param string $sitePath
* @param string $siteName
* @param string $uri
* @return bool
*/
public function serves($sitePath, $siteName, $uri)
{
return $this->isStaticFile($sitePath, $siteName, $uri);
}
/**
* Serve the static file at the given path.
*
* @param string $staticFilePath
* @param string $sitePath
* @param string $siteName
* @param string $uri
* @return void
*/
public function serveStaticFile($staticFilePath, $sitePath, $siteName, $uri)
{
header('Access-Control-Allow-Origin: *');
parent::serveStaticFile($staticFilePath, $sitePath, $siteName, $uri);
}
}
@milesw
Copy link
Author

milesw commented Dec 3, 2016

Save as: ~/.valet/Drivers/StaticCorsValetDriver.php

Explanation:
During development I needed to serve JSON files from a different directory and therefore a different Valet domain. The application at domain1.dev needed to make AJAX requests for domain2.dev/file.json, and the browser was blocking this without appropriate headers returned by domain2.dev. Thankfully Valet makes it really easy to add custom drivers.

@vesper8
Copy link

vesper8 commented May 17, 2017

@milesw

I tried your solution but I can't get it to work on font files referenced inside css files

I describe my issue in detail here: laravel/valet#377

Would appreciate your feedback if you have any idea what's causing this and how to fix it?

Thanks!

@sdevore
Copy link

sdevore commented Jan 11, 2023

Thank You!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment