【发布时间】:2015-07-23 18:53:40
【问题描述】:
我正在使用一个共享主机,它使用 cPanel 作为其控制面板,并且在 cPanel 中 public_html 是默认根目录,因此我无法让我的 Laravel 应用程序正常工作。
有没有办法让 Laravel 使用 public_html 而不是公用文件夹?
【问题讨论】:
标签: php apache2 laravel-5 cpanel
我正在使用一个共享主机,它使用 cPanel 作为其控制面板,并且在 cPanel 中 public_html 是默认根目录,因此我无法让我的 Laravel 应用程序正常工作。
有没有办法让 Laravel 使用 public_html 而不是公用文件夹?
【问题讨论】:
标签: php apache2 laravel-5 cpanel
通过简单的搜索很容易找到。
在您的 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');
});
}
【讨论】:
[ErrorException] chdir(): No such file or directory (errno 2) 。
Turn On The Lights-section 之后添加。然后您应该将path.public 更新为path.public_html。至少,这对我有用。 :)
public 目录。
如果罗伯特的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';
});
}
【讨论】:
public_html而不是public_http吗?
服务器
主题中描述的方法工作得很好,所以修改 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 watch 或 npm 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 值的相对路径。
编码愉快。
【讨论】:
./resources/js/components/SetBgImg.vue?vue&type=script&lang=js&":,应该是 ./../projectname/resources/js/components/SetBgImg.vue?vue&type=script&lang=js&": 如何解决这个问题?
npm run 以使更改生效。
去这个地址:
/app/Providers/AppServiceProvider.php
并将此代码附加到文件末尾:
public function register()
{ $this->app->bind('path.public', function() {
return realpath(base_path().'/../public_html');
});
}
【讨论】:
只是想更新所有以前的答案,如果您的 public_html 不在 laravel 文件夹中,那么您需要使用此代码:
$this->app->bind('path.public', function() {
return realpath(base_path().'/../public_html');
});
【讨论】:
这些答案不适用于 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。
【讨论】:
简单根目录创建.htaccess文件并添加代码:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
【讨论】:
对于那些需要更改公共路径以使其也可供 Artisan 使用的用户,请将以下内容添加到 bootstrap/app.php,就在最后一个 singleton 方法之后:
$app->bind('path.public', function () {
return base_path("<your public directory name>");
});
【讨论】:
我花了两天时间才弄清楚,但最后我通过执行后续步骤将我的 Laravel 项目部署到了我的 cPanel 虚拟主机:
if ($uri !== '/' && file_exists(__DIR__ . '/public_html' .$uri)) {
return false;
}
require_once __DIR__ . '/public_html/index.php';
mix.js('resources/js/app.js', 'public_html/js')
.postCss('resources/css/app.css', 'public_html/css', [
//
]);
如果您有任何问题,请随时与我联系 :)
【讨论】:
这不像绑定那么容易。 ferrolho 在这里https://laracasts.com/discuss/channels/general-discussion/where-do-you-set-public-directory-laravel-5 提供了最简洁和更详细的答案
但最快的答案是创建一个名为 public 的符号链接,指向 public_html 并将您的 index.php 放在最后一个。
【讨论】:
在bootstrap/app.php:
添加
$app->bind('path.public', function() {
return __DIR__;
});
紧接着
$app = new Illuminate\Foundation\Application(
realpath(__DIR__)
);
修复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';
在.gitignore,更改
/public/hot
/public/storage
到
/public_html/hot
/public_html/storage
在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() 函数寻找修复程序,该函数仍然损坏...
【讨论】:
对我来说,在我更改服务器设置中的根公用文件夹之前,这一切都不起作用。在生产中,它位于/etc/nginx/sites-enabled/<your_site_name>。使用文本编辑器对其进行编辑,然后滚动直到看到指向/public 文件夹的根路径。将其更改为您的新公用文件夹并重新启动服务器。在本地,我必须在通过 SSH 连接到 Vagrant 后更改 Homestead.yaml 和 /etc/nginx/sites-enabled/<your_site_name> 中的路径。 vagrant reload --provision 确保它流行起来。然后我还编辑了index.php,并在服务提供者中注册了一条新路径以防万一,但没有它似乎也可以工作。不过,我没有在 Laravel 中使用任何 3rd 方依赖项,所以我不知道这是否适用于这种情况。
【讨论】:
对我来说最简单的解决方案是创建符号链接。
public_html 文件夹。ssh连接到您的服务器。~/laravel 文件夹中。~/ 文件夹中:rm -r public_html。此命令将删除 public_html 文件夹及其所有内容。ln -s $(pwd)/laravel/public $(pwd)/public_html。 $(pwd) 将被替换为服务器根目录的绝对路径。【讨论】: