【问题标题】:How to change public folder to public_html in laravel 5如何在 laravel 5 中将公用文件夹更改为 public_html
【发布时间】:2015-07-23 18:53:40
【问题描述】:

我正在使用一个共享主机,它使用 cPanel 作为其控制面板,并且在 cPanel 中 public_html 是默认根目录,因此我无法让我的 Laravel 应用程序正常工作。

有没有办法让 Laravel 使用 public_html 而不是公用文件夹?

【问题讨论】:

    标签: php apache2 laravel-5 cpanel


    【解决方案1】:

    通过简单的搜索很容易找到。

    见:https://laracasts.com/discuss/channels/general-discussion/where-do-you-set-public-directory-laravel-5

    在您的 index.php 中添加以下 3 行。

    /*
    |--------------------------------------------------------------------------
    | Turn On The Lights
    |--------------------------------------------------------------------------
    |
    | We need to illuminate PHP development, so let us turn on the lights.
    | This bootstraps the framework and gets it ready for use, then it
    | will load up this application so that we can run it and send
    | the responses back to the browser and delight our users.
    |
    */
    
    $app = require_once __DIR__.'/../bootstrap/app.php';
    
    // set the public path to this directory
    $app->bind('path.public', function() {
        return __DIR__;
    });
    

    编辑:


    正如 Burak Erdem 所提到的,另一种选择(更可取)是将其放入 \App\Providers\AppServiceProvider register() 方法中。

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        // ...
    
        $this->app->bind('path.public', function() {
            return base_path('public_html');
        });
    }
    

    【讨论】:

    • 它对我不起作用:致命错误:在...中的非对象上调用成员函数 make()
    • 仅仅通过添加?我可以在没有任何错误的情况下切换它。你能发布完整的错误消息吗,它并没有告诉我太多。
    • @Robert 当我使用你的解决方案时,它给了我错误[ErrorException] chdir(): No such file or directory (errno 2)
    • 我知道已经有一段时间了,但这个问题在谷歌中仍然排名很高。因此我想指出,对我来说,这些代码行应该在Turn On The Lights-section 之后添加。然后您应该将path.public 更新为path.public_html。至少,这对我有用。 :)
    • 这不适用于 laravel-mix,它仍然会将资产编译到 public 目录。
    【解决方案2】:

    如果罗伯特的index.php 解决方案不适合您,您也可以在Application Service Provider (App\Providers\AppServiceProvider.php) 注册以下代码。

    <?php
    
    namespace App\Providers;
    
    use Illuminate\Support\ServiceProvider;
    
    public function register()
    {
        $this->app->bind('path.public', function() {
            return base_path().'/public_http';
        });
    }
    

    【讨论】:

    • 我更喜欢使用这种方法。它在 Laravel 5.3 中工作。
    • 我更喜欢这个,因为 index.php 更有可能在框架更新时发生变化
    • 这个答案应该是public_html而不是public_http吗?
    • 我正在使用 laravel 5.7,但是这不起作用,我需要做什么。
    【解决方案3】:

    服务器

    主题中描述的方法工作得很好,所以修改 App\Providers\AppServiceProvider.php 注册方法应该可以完成这项工作:

    <?php
    
    namespace App\Providers;
    
    use Illuminate\Support\ServiceProvider;
    
    public function register()
    {
        $this->app->bind('path.public', function() {
            return base_path() . '/public_http';
        });
    }
    

    本地 php artisan 服务开发

    但是,您还会遇到另一个问题。如果您在本地机器上开发您的应用程序并且您使用php artisan serve 命令来为您的应用程序提供服务,那么您将仅使用上述语法来破坏它。您仍然需要调整存在于主目录中的 server.php 文件。编辑其中的内容,将每次出现的/public替换为/public_html,所以看起来是这样的:

    <?php
    
    /**
     * Laravel - A PHP Framework For Web Artisans
     *
     * @package  Laravel
     * @author   Taylor Otwell <taylor@laravel.com>
     */
    
    $uri = urldecode(
        parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)
    );
    
    // This file allows us to emulate Apache's "mod_rewrite" functionality from the
    // built-in PHP web server. This provides a convenient way to test a Laravel
    // application without having installed a "real" web server software here.
    if ($uri !== '/' && file_exists(__DIR__.'/public_html'.$uri)) {
        return false;
    }
    
    require_once __DIR__.'/public_html/index.php';
    

    之后。只需停止您的服务器并使用php artisan serve 重新加载它。

    前端 laravel-mix 开发

    如果你使用 webpack 和 laravel-mix 来生成你的 css 和 js 文件,那么这也需要一些更新。如果不调整 webpack.mix.js,你最终会在 npm run watchnpm run production 上得到类似的东西:

    .
    ..
    public
      _html
        js
      public_html
        css
    public_html
    

    所以它会弄乱你的代码。为了澄清这一点,您必须提供 webpack.mix.js 文件的公共路径。它可能看起来像这样:

    const mix = require('laravel-mix');
    
    mix.setPublicPath('public_html/');
    mix.js('resources/js/app.js', 'js')
    mix.sass('resources/sass/app.scss', 'css');
    

    这会将公共目录的默认定义从 public 更改为 public_html 并且下一行提供了您的 setPublicPath 值的相对路径。

    编码愉快。

    【讨论】:

    • 感谢您的回答!这很好用,除了 vue。它在正确的文件夹中编译 css 和 js(在我的情况下是 /../public_html),但它们不起作用。当它编译 app.js 时,它不考虑 .vue 文件的路径:./resources/js/components/SetBgImg.vue?vue&amp;type=script&amp;lang=js&amp;":,应该是 ./../projectname/resources/js/components/SetBgImg.vue?vue&amp;type=script&amp;lang=js&amp;": 如何解决这个问题?
    • 不要忘记重新启动npm run 以使更改生效。
    • 我不使用 vue,但现在这个答案确实适用于 localhost,而且我每次该死的时候都不需要将资产复制粘贴到云中同步项目的公共文件夹中。
    【解决方案4】:

    去这个地址:

    /app/Providers/AppServiceProvider.php
    

    并将此代码附加到文件末尾:

    public function register()
    {   $this->app->bind('path.public', function() {
        return realpath(base_path().'/../public_html');
      });
    }
    

    【讨论】:

      【解决方案5】:

      只是想更新所有以前的答案,如果您的 public_html 不在 laravel 文件夹中,那么您需要使用此代码:

      $this->app->bind('path.public', function() {
         return realpath(base_path().'/../public_html');
      });
      

      【讨论】:

        【解决方案6】:

        这些答案不适用于 laravel 5.5,但我自己的方法可以帮助你。

        第一步:将除公共文件外的所有文件丢弃到服务器的主目录中。

        第2步:公开文件到服务器上的public_html文件。

        第 3 步:从 public_html 中提取 index.php 文件并像这样进行更改。

        步骤 3A:

        原始 -> require __DIR__.'/../vendor/autoload.php';

        更改 -> require __DIR__.'/../**created_folder**/vendor/autoload.php';

        原创 -> $app = require_once __DIR__.'/../bootstrap/app.php';

        更改 -> $app = require_once __DIR__.'/../**Created_folder**/bootstrap/app.php';

        第四步:创建 symlinkcreate.php

        步骤 4A:<?php symlink('/home/**server_directory**/**created_folder**/storage/app/public','/home/**server_directory**/public_html/storage');

        步骤 4B:yourwebsite.com/symlinkcreate.php 访问

        步骤 4C:symlinkcreate.php 删除你的服务器。

        最后,目录结构如下:

        /etc
        /logs
        /lscache
        /mail
        /Your_Created_Folder
          ../LARAVEL_FOLDERS
        /public_html
          ../css
          ../js
          ../.htaccess
          ../index.php
          ../web.config
        /ssl
        /tmp
        

        完成。

        Laravel 5.5 public_html sorunu için bu cevabı gönül rahatlığıyla kullanabilirsiniz。

        【讨论】:

          【解决方案7】:

          简单根目录创建.htaccess文件并添加代码:

          <IfModule mod_rewrite.c>
          RewriteEngine On
          RewriteRule ^(.*)$ public/$1 [L]
          </IfModule>
          

          【讨论】:

            【解决方案8】:

            对于那些需要更改公共路径以使其也可供 Artisan 使用的用户,请将以下内容添加到 bootstrap/app.php,就在最后一个 singleton 方法之后:

            $app->bind('path.public', function () {
                return base_path("<your public directory name>");
            });
            

            【讨论】:

              【解决方案9】:

              我花了两天时间才弄清楚,但最后我通过执行后续步骤将我的 Laravel 项目部署到了​​我的 cPanel 虚拟主机:

              1. 修改server.php文件如下:
              if ($uri !== '/' && file_exists(__DIR__ . '/public_html' .$uri)) {
                  return false;
              }
              
              require_once __DIR__ . '/public_html/index.php';
              
              1. 修改 webpack.mix.js 如下:
              mix.js('resources/js/app.js', 'public_html/js')
                  .postCss('resources/css/app.css', 'public_html/css', [
                      //
                  ]);
              
              1. 将Laravel-project中“public”文件夹的名称改为“public_html”,并将代码上传到根目录cPanel。确保您有来自当前 public_html 目录的备份。如果您的主机上有正确的 PHP 版本并编辑了 .env 文件,那么一切都应该正常工作。

              我的 cPanel 根目录(来自文件管理器)如下所示:

              而 public_html(最初是公共目录)看起来像这样:

              如果您有任何问题,请随时与我联系 :)

              【讨论】:

                【解决方案10】:

                这不像绑定那么容易。 ferrolho 在这里https://laracasts.com/discuss/channels/general-discussion/where-do-you-set-public-directory-laravel-5 提供了最简洁和更详细的答案

                但最快的答案是创建一个名为 public 的符号链接,指向 public_html 并将您的 index.php 放在最后一个。

                【讨论】:

                  【解决方案11】:
                  1. bootstrap/app.php:

                    添加

                    $app->bind('path.public', function() {
                      return __DIR__;
                    });
                    

                    紧接着

                    $app = new Illuminate\Foundation\Application(
                      realpath(__DIR__)
                    );
                    
                  2. 修复server.php

                    改变

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

                    if ($uri !== '/' && file_exists(__DIR__.'/public_html'.$uri)) {
                      return false;
                    }
                    
                    require_once __DIR__.'/public_html/index.php';
                    
                  3. .gitignore,更改

                    /public/hot
                    /public/storage
                    

                    /public_html/hot
                    /public_html/storage
                    
                  4. webpack.mix.js,更改

                    mix.js('resources/js/app.js', 'public/js')
                      .sass('resources/sass/app.scss', 'public/css');
                    

                    mix.js('resources/js/app.js', 'public_html/js')
                      .sass('resources/sass/app.scss', 'public_html/css');
                    

                  我仍在为 asset() 函数寻找修复程序,该函数仍然损坏...

                  【讨论】:

                    【解决方案12】:

                    对我来说,在我更改服务器设置中的根公用文件夹之前,这一切都不起作用。在生产中,它位于/etc/nginx/sites-enabled/&lt;your_site_name&gt;。使用文本编辑器对其进行编辑,然后滚动直到看到指向/public 文件夹的根路径。将其更改为您的新公用文件夹并重新启动服务器。在本地,我必须在通过 SSH 连接到 Vagrant 后更改 Homestead.yaml/etc/nginx/sites-enabled/&lt;your_site_name&gt; 中的路径。 vagrant reload --provision 确保它流行起来。然后我还编辑了index.php,并在服务提供者中注册了一条新路径以防万一,但没有它似乎也可以工作。不过,我没有在 Laravel 中使用任何 3rd 方依赖项,所以我不知道这是否适用于这种情况。

                    【讨论】:

                      【解决方案13】:

                      对我来说最简单的解决方案是创建符号链接。

                      1. 备份您的public_html 文件夹。
                      2. 通过ssh连接到您的服务器。
                      3. 在我的特定场景中,整个应用程序位于~/laravel 文件夹中。
                      4. 现在您必须在 ~/ 文件夹中:
                      5. 运行rm -r public_html。此命令将删除 public_html 文件夹及其所有内容。
                      6. 建立符号链接ln -s $(pwd)/laravel/public $(pwd)/public_html$(pwd) 将被替换为服务器根目录的绝对路径。
                      7. 现在您应该可以看到想要的结果了。

                      【讨论】:

                        猜你喜欢
                        • 2021-07-23
                        • 1970-01-01
                        • 1970-01-01
                        • 2016-12-23
                        • 2021-08-10
                        • 1970-01-01
                        • 1970-01-01
                        • 1970-01-01
                        • 2021-07-11
                        相关资源
                        最近更新 更多