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.There 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:
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'; } }
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
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!