【问题标题】:Laravel 5 on shared hosting - wrong public_path()共享主机上的 Laravel 5 - 错误的 public_path()
【发布时间】:2015-04-27 09:35:44
【问题描述】:

我将 Laravel 5 项目部署到共享主机帐户,将应用程序文件放在 www 文件夹之外,仅将公共文件夹放在 www 文件夹内。就像这里解释的那样,最好的答案是:Laravel 4 and the way to deploy app using FTP... without 3. 因为这个文件在 Laravel 5 中不存在。

现在,当我在控制器内部调用 public_path() 时,我得到类似 my-appfiles-outside-of-www/public 而不是 www/公开。有谁知道为什么我在这里没有正确的路径?我可以在某个地方更改它吗? 谢谢!

【问题讨论】:

    标签: php laravel-5 shared-hosting


    【解决方案1】:

    您可以使用容器覆盖 public_path。 示例:

    App::bind('path.public', function() {
        return base_path().'/public_html';
    });
    

    【讨论】:

    【解决方案2】:
    $app->bind('path.public', function() {
      return __DIR__;
    });
    

    把上面的代码写在这段代码上面的public_html\index.php中

    $kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);
    

    【讨论】:

      【解决方案3】:

      有许多解决方案,例如更改 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)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-01-13
        • 2016-04-10
        • 2015-10-04
        • 2018-06-07
        • 2016-05-21
        • 2021-10-18
        相关资源
        最近更新 更多