php - 使用 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。我真的很想在本地的同一个地方使用它并生活。我希望这是有道理的。
任何帮助或见解表示赞赏。谢谢!
解决方案
您可能需要为它创建一个自定义代客驱动程序。
去:~/.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
推荐阅读
- php - 自动创建 Laravel 项目失败并出现错误:TTY 模式需要 /dev/tty 可以读/写
- angular - 无法选择 Angular Material 芯片/芯片并表明该芯片已被选中?
- python-3.x - Pandas 在 groupby 之后移动多列导致按字母顺序移动列
- xero-api - 联系方式 - 默认货币
- javascript - 使用 Lodash 和 Moment 从单个日期开始的日/小时分组数组
- python-3.x - 使用 Python 将 sigmoid 函数(“S”形曲线)拟合到数据中
- python - 如何使用 sklearn 或 scipy 聚类方法打印聚类的层次结构?
- r - 在 Amazon AWS Ec2 上运行 RStudio
- jquery - 当没有帖子时,为什么计数器在第 1 篇帖子中增加 n 次
- database-design - 桌面应用程序之间的数据共享和通信