我在这里包含了两种不同的选项,一种用于仅更改公共目录名称,另一种用于将核心文件放入其自己的目录中:
第一个选项:对于 laravel 9,只需重命名 public/ 目录:
- 将公用文件夹重命名为
public_html
(或任何想要的名称)
- Edit
AppServiceProviver.php
并在注册方法中添加以下代码:
$this->app->bind('path.public', function() {
return realpath(base_path().'/public_html');
});
- Add
publicDirectory: 'public_html'
to laravel_core/vite.config.js
在plugins / laravel对象中,例如。
export default defineConfig({
plugins: [
laravel({
publicDirectory: 'public_html', // This is the only new line
input: [
'resources/sass/app.scss',
'resources/js/app.js',
],
refresh: true,
}),
],
});
第二个选项:将 laravel 核心文件分离到它们自己的目录中,并重命名 public/ 目录,对于 laravel 9:
- 将公用文件夹重命名为
public_html
(或任何想要的名称)
- 创建一个
laravel_core
目录并移动除public_html
进入 laravel_core 文件夹。
- Edit
index.php
in public_html
文件夹并查找/替换__DIR__.'/../
with __DIR__.'/../laravel_core/
(3例)
- Edit
AppServiceProviver.php
并在注册方法中添加以下代码:
$this->app->bind('path.public', function() {
return realpath(base_path().'/../public_html');
});
- Add
publicDirectory: 'public_html'
to laravel_core/vite.config.js
在plugins / laravel对象中,例如。
export default defineConfig({
plugins: [
laravel({
publicDirectory: '../public_html',
input: [
'resources/sass/app.scss',
'resources/js/app.js',
],
refresh: true,
}),
],
});
Laravel 10 的重要说明
在 Laravel 10 中,您还需要将此行添加到bootstrap/app.php
, 之后$app
对象在第 14 行实例化:
$app = new Illuminate\Foundation\Application(
$_ENV['APP_BASE_PATH'] ?? dirname(__DIR__)
);
// Add this below:
$app->usePublicPath($app->basePath('../public_html'));
请注意,我已经使用了../public_html
在这里,因为我有laravel_core
文件分隔到自己的目录中,如上面的第二个选项所示,但如果您只是更改public
目录名称,然后使用它:
$app->usePublicPath($app->basePath('public_html'));