Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Valet Driver for Craft CMS (2.x) that tries to resolve filenames using static asset versioning, per
class CacheBustingCraftValetDriver extends ValetDriver
* 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 is_dir($sitePath.'/craft');
* Determine if the incoming request is for a static file.
* @param string $sitePath
* @param string $siteName
* @param string $uri
* @return string|false
public function isStaticFile($sitePath, $siteName, $uri)
if ($this->isActualFile($staticFilePath = $sitePath.'/public'.$uri)) {
return $staticFilePath;
$segments = explode('/', $uri);
$filename = end($segments);
unset($segments[count($segments) - 1]);
$filename = explode('.', $filename);
if (count($filename) > 1) unset($filename[count($filename) - 2]);
$filename = join('.', $filename);
$segments[] = $filename;
$bustedUri = join('/', $segments);
if ($this->isActualFile($staticFilePath = $sitePath.'/public'.$bustedUri)) {
return $staticFilePath;
return false;
* Get the fully resolved path to the application's front controller.
* @param string $sitePath
* @param string $siteName
* @param string $uri
* @return string
public function frontControllerPath($sitePath, $siteName, $uri)
// Default index path
$indexPath = $sitePath.'/public/index.php';
$scriptName = '/index.php';
// Check if the first URL segment matches any of the defined locales
$locales = [
$parts = explode('/', $uri);
if (count($parts) > 1 && in_array($parts[1], $locales)) {
$indexPath = $sitePath.'/public/'. $parts[1] .'/index.php';
$scriptName = '/' . $parts[1] . '/index.php';
$_SERVER['SCRIPT_NAME'] = $scriptName;
$_SERVER['PHP_SELF'] = $scriptName;
return $indexPath;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment