首页 > 解决方案 > 使用 Laravel Valet 为非 Laravel 项目提供服务时,如何访问公共目录中的 PHP 文件?

问题描述

我使用 Laravel Valet 在本地为多个 Laravel、WordPress 和 vanilla PHP 应用程序提供服务。

我有一个特定的应用程序,它是我通过 Valet 从http://myapplication.test访问的普通 PHP

这个应用程序有一个 /public/index.php 文件,当我转到测试 URL 时会自动显示该文件,因为 Valet 知道要查看公共目录。

我遇到的问题是,当我放置 /public/test.php 文件并尝试通过访问http://myapplication.test/test.php来访问它时……它不提供该文件。相反,我被迫去http://myapplication.test/public/test.php

有什么办法可以解决这个问题吗?

这导致我在我的代码库中做一个丑陋的重复工作。在实时项目中,访问 www.liveurl.com/test.php 是可行的,因此我必须在实际的公共目录中放置第二个公共目录,以便在代码库中的任何地方将文件引用为 /public/test.php。我真的很想在本地的同一个地方使用它并生活。我希望这是有道理的。

任何帮助或见解表示赞赏。谢谢!

标签: phplaravelvalet

解决方案


您可能需要为它创建一个自定义代客驱动程序

去:~/.config/valet/Drivers

创建PhpValetDriver.php文件并粘贴此代码:

<?php

class PhpValetDriver extends ValetDriver
{

     private $site_folder = '/public';

    /**
     * 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 (!file_exists($sitePath.'/artisan') &&
              file_exists($sitePath.$this->site_folder.'/index.php')) {

             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($staticFilePath = $sitePath.$this->site_folder.$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)
    {
        $path = $sitePath.$this->site_folder;

        if ($uri == '/') {
            return $path.'/index.php';
        }

        return strpos($uri, '.php') ? $path.$uri : $path.$uri.'.php';
    }
}

接着:valet restart


推荐阅读