Running Flarum on Laravel Valet

Laravel Valet is a great tool for experimenting with PHP code on Mac OS X. However, its configuration is a little bit different to conventional HTTP servers (Apache, nginx, etc.). Instead of URL rewriting in configuration files, Valet uses a modular approach by defining URL handling in PHP classes.

There is a configuration file in ~/.valet/Drivers called SampleValetDriver.php, which contains three methods: serves,isStaticFile and frontControllerPath.

In serves you should use the signature of an application to determine whether Valet should serve the application using this configuration.

public function serves($sitePath, $siteName, $uri){
  return is_dir($sitePath.'/vendor/flarum')
         && file_exists($sitePath.'/flarum');
}

In isStaticFile you should tell Valet whether a given URL points to a static file, and its real path.

public function isStaticFile($sitePath, $siteName, $uri){
  if ($this
      ->isActualFile($staticFilePath = $sitePath.$uri)) {
    return $staticFilePath;
  }

  return false;
}

frontControllerPath is the most similar part to Apache's mod_rewrite and nginx's try_uri. Here you rewrites requests to controllers to their handler, as illustrated below.

public function frontControllerPath($sitePath, $siteName, $uri)
{
  if (strpos($uri,'/admin') === 0) {
    return $sitePath.'/admin.php';
  }
  if (strpos($uri,'/api') === 0) {
    return $sitePath.'/api.php';
  }

  return $sitePath.'/index.php';
}

The final result is below. Save this as FlarumValetDriver.php in ~/.valet/Drivers, and you are good to go.

<?php

class FlarumValetDriver 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.'/vendor/flarum') && file_exists($sitePath.'/flarum');
     }
/**
 * 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.$uri)) {
            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)
   {
       if (strpos($uri,'/admin') === 0) {
           return $sitePath.'/admin.php';
       }
       if (strpos($uri,'/api') === 0) {
           return $sitePath.'/api.php';
       }

       return $sitePath.'/index.php';
   }
}

Gist