Home >PHP Framework >Laravel >Detailed explanation of how to configure and run the Flarum forum system in the Mac development environment Laravel Valet

Detailed explanation of how to configure and run the Flarum forum system in the Mac development environment Laravel Valet

藏色散人
藏色散人forward
2020-04-28 15:03:502155browse

The following tutorial column will introduce you to the implementation method of configuring and running the Flarum forum system in the Mac development environment Laravel Valet. I hope it will be helpful to friends in need!

Laravel Valet is a minimalist development environment provided for Mac OS X. However, the configuration of Valet is somewhat different from traditional HTTP servers (Apache, Nginx, etc.). To rewrite URLs in configuration files, Valet defines URL handling in a modular way in PHP classes. Since the default directory structures of Flarum and Laravel are different, we need to define its access configuration in Valet.

Detailed explanation of how to configure and run the Flarum forum system in the Mac development environment Laravel ValetThere is a default configuration file SampleValetDriver.php in the ~/.valet/Drivers directory, which contains three methods: serves, isStaticFile and frontControllerPath. We now need to configure our own configuration file FlarumValetDriver.php, and follow these three methods to write our own driver extension:

cp SampleValetDriver.php FlarumValetDriver.php

Open FlarumValetDriver.php, first rewrite the serves method, in which we need to specify Valet Check whether the corresponding Flarum application directory under the Web root directory (mine here is flarum, if it is different, you need to change it to your own Flarum application directory) exists. This is somewhat similar to defining root in Nginx:

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

Next, in The isStaticFile method determines whether the given URL points to a static file and the static file does exist. This is similar to how we define static file access in nginx:

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

    return false;
}

Finally rewrite the frontControllerPath method, which is similar to mod_rewrite in Apache And try_uri in Nginx, here we can rewrite the request access path:

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 as follows, we save it under ~/.valet/Drivers:

<?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.&#39;/vendor/flarum&#39;) && file_exists($sitePath.&#39;/flarum&#39;);
    }

    /**
     * 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&#39;s front controller.
     *
     * @param  string  $sitePath
     * @param  string  $siteName
     * @param  string  $uri
     *
     * @return string
     */
    public function frontControllerPath($sitePath, $siteName, $uri)
    {
        if (strpos($uri,&#39;/admin&#39;) === 0) {
            return $sitePath.&#39;/admin.php&#39;;
        }
        if (strpos($uri,&#39;/api&#39;) === 0) {
            return $sitePath.&#39;/api.php&#39;;
        }

        return $sitePath.&#39;/index.php&#39;;
    }
}

This way You can now access all Falrum routes normally. If an access error is reported:

Call to undefined method FlarumValetDriver::isActualFile() in /Users/sunqiang/.valet/Drivers/FlarumValetDriver.php on line 29

This is because Valet has not been upgraded to the latest version. Just execute the following command to upgrade Valet:

composer global update

Original address: https://xueyuanjun.com/ post/5679

The above is the detailed content of Detailed explanation of how to configure and run the Flarum forum system in the Mac development environment Laravel Valet. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:xueyuanjun.com. If there is any infringement, please contact admin@php.cn delete