Created
December 3, 2016 08:02
-
-
Save milesw/832ffe73c1e9ff3af81f5148f8e0ee45 to your computer and use it in GitHub Desktop.
Laravel Valet driver that serves static files with CORS header
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
} | |
} |
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!
Thank You!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.