zeshan-a
7/18/2017 - 2:12 PM

Valet site change document root using Valet Custom Driver

Valet site change document root using Valet Custom Driver

<?php

// Change 'site_folder_name' with your valet site's name, e.g., gutenberg
// Change '/root-folder' with the root folder you want to start
// when you type e.g., gutenberg.dev
class MyValetDriver 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 )
    {
        if ( $siteName === 'site_folder_name' ) {
            if ( file_exists( $sitePath . '/' ) ) {
                return true;
            }
        }

        return false;
    }

    /**
     * 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 ( file_exists( $sitePath . $uri ) && ! is_dir( $sitePath . $uri ) && pathinfo( $sitePath . $uri )['extension'] != 'php' ) {
            return $sitePath . $uri;
        }

        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 ( $siteName === 'site_folder_name' ) {
            $uri = '/root-folder' . $uri;
        }

        // The requested resource is a PHP file.
        if ( file_exists ( $sitePath . $uri ) && isset( pathinfo( $sitePath . $uri )['extension'] ) ) 
        {
            if ( pathinfo( $sitePath . $uri )['extension'] != 'php' ) {

                // Set the content type to 'text/css'
                if ( pathinfo( $sitePath . $uri )['extension'] == 'css' ) {
                    header( 'Content-type: text/css' );
                }

                return $sitePath . $uri;
            }
        }

        // The requested resource is a directory and contains a child ‘index.php’ file.
        if ( file_exists ( $sitePath . $uri . '/index.php' ) ) 
        {
            return $sitePath . $uri . '/index.php';
        }

        // If the path does not exist, serve this default file.
        return $sitePath . '/excludes.txt';
    }
}