如何在laravel 5中将公用文件夹更改为public_html

Rav*_*ina 43 php apache2 cpanel laravel-5

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

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

Rob*_*ert 64

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

请参阅: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__;
});
Run Code Online (Sandbox Code Playgroud)

编辑:


正如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');
    });
}
Run Code Online (Sandbox Code Playgroud)

  • 我知道它已经有一段时间了,但这个问题仍然排在谷歌的高位.因此,我想指出,对我来说,这些代码行应该在"Turn On The Lights"部分之后添加.然后你应该将`path.public`更新为`path.public_html`.至少,这对我有用.:) (2认同)

Bur*_*dem 19

如果Robert的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';
    });
}
Run Code Online (Sandbox Code Playgroud)


小智 8

从 Laravel 版本 10 开始,有一种新的方法来更改路径。因此,在您的服务提供商中执行以下操作:

public function register()
{
    $app->usePublicPath(__DIR__.'/../../public_html');
    /* old version code
    $this->app->bind('path.public', fn() => base_path() . '/public_html';);
    */
}
Run Code Online (Sandbox Code Playgroud)


Dav*_*vid 6

服务器

主题中描述的方法可以正常工作,因此modyfing 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';
    });
}
Run Code Online (Sandbox Code Playgroud)

本地php artisan服务开发

但是,您还会遇到另外一个问题。如果您要在本地计算机上开发应用程序,并且正在使用php artisan serve命令来为您的应用程序提供服务,那么您只会使用上述语法来破坏它。您仍然需要调整主目录中存在的server.php文件。编辑它的内容,并替换每次出现的/publicto /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';
Run Code Online (Sandbox Code Playgroud)

之后。只需停止服务器,然后使用重新加载即可php artisan serve。

前端laravel-mix开发

如果您使用webpack和laravel-mix生成CSS和JS文件,则还需要进行一些更新。如果不调整webpack.mix.js,您最终会在npm run watch或上得到类似这样的内容npm run production:

.
..
public
  _html
    js
  public_html
    css
public_html
Run Code Online (Sandbox Code Playgroud)

因此,它将弄乱您的代码。为了澄清这一点,您必须提供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');
Run Code Online (Sandbox Code Playgroud)

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

快乐的编码。


Ale*_*lex 5

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

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


小智 5

转到这个地址:

/app/Providers/AppServiceProvider.php
Run Code Online (Sandbox Code Playgroud)

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

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