如何在 laravel 8 中将 public 文件夹更改为 public_html ?

Abd*_*mun 5 deployment cpanel laravel

我想将我的应用程序部署在 Cpanel 上的共享托管上,其中主文档根目录有public_htmlLaravel 项目public

Abd*_*mun 8

您必须按照 2 个步骤将应用程序的公共文件夹更改为 public_html,然后您就可以部署它或执行任何您可以执行的操作:)

  1. 编辑\App\Providers\AppServiceProviderregister() 方法并添加此代码。

     // set the public path to this directory
     $this->app->bind('path.public', function() {
         return base_path().'/public_html';
     });
    
    Run Code Online (Sandbox Code Playgroud)
  2. 打开server.php可以看到这段代码

    if ($uri !== '/' && file_exists(__DIR__.'/public'.$uri)) {
       return false;
    }
    
    require_once __DIR__.'/public/index.php';
    
    Run Code Online (Sandbox Code Playgroud)

只需将其替换为:

  if ($uri !== '/' && file_exists(__DIR__.'/public_html'.$uri)) {
   return false;
  }

  require_once __DIR__.'/public_html/index.php';
Run Code Online (Sandbox Code Playgroud)

然后为您的应用程序提供服务php artisan serve,您还可以将其部署在主文档根 public_html 的 Cpanel 共享主机上

  • 进行此更改后,我在打开网站时仍然收到 404。这可能是什么原因? (2认同)

Jas*_*lis 6

您可以将rename公共目录设置为您想要的任何内容,并告诉 Laravel 使用当前目录路径作为公共路径。只需导航到文件夹index.php内部public(或您将其重命名为的任何内容)并在定义后添加以下代码$app

$app = require_once __DIR__.'/../bootstrap/app.php';

/* *** Add this code: *** */
$app->bind('path.public', function() {
    return __DIR__;
});
/* ********************** */
Run Code Online (Sandbox Code Playgroud)


F K*_*Ing 5

我看到这篇文章解释了如何去做。简而言之,转到public_html/index.php$app->bind('path.public', function() { return __DIR__; });在创建 $app 之后添加。

要修复 CLI 模式或 Artisan 脚本中使用的脚本的路径,您应该将以下代码添加到文件/bootstrap/app.php

$app->bind('path.public', function() { return base_path().'/public_html'; });
Run Code Online (Sandbox Code Playgroud)

确保在创建 $app 之后添加此行。

更新 在 laravel 10 中,您应该更新代码以调用 Illuminate\Foundation\Application 对象提供的 usePublicPathhave 方法。将此行添加到您的public_html/index.php文件中

app()->usePublicPath(__DIR__);
Run Code Online (Sandbox Code Playgroud)

您可以在此处查看更新的详细信息

  • 你通过 `laravel 10 更新`救了我:))) (2认同)