有许多解决方案,例如更改 index.php 或使用 ioc 容器接受的解决方案。
在我看来,这个 SO 问题中描述了最有效的解决方案:
Laravel 5 change public_path()
为什么它比其他解决方案更好?因为它适用于常规的 http 请求,使用 artisan 和使用 phpunit 进行测试!例如。对 index.php 的更改仅适用于 http 请求,但在使用 PHPunit 时会导致失败(在我的情况下:文件 (...) 未在资产清单中定义)。
对该解决方案的简短回顾:
第 1 步:在文件中:bootstrap/app.php 将 $app 变量的第一个声明从:
$app = new Illuminate\Foundation\Application(
realpath(__DIR__.'/../')
);
到:
$app = new App\Application(
realpath(__DIR__.'/../')
);
这指向您自己的自定义应用程序类,我们将在步骤 2 中创建它。
第二步:在app文件夹下创建Application.php文件:
<?php namespace App;
class Application extends \Illuminate\Foundation\Application
{
public function publicPath()
{
return $this->basePath . '/../public_html';
}
}
请务必根据您的需要更改路径。该示例假定目录结构如下:
°°laravel_app
°°°°应用
°°°°自举
°°°°配置
°°°°...
°°public_html
如果是 OP,路径应该是
return $this->basePath . '/../www/public';
(从 laravel_app 向上一级,然后向下进入 www/public)