php 如何在 Laravel 5 中将公共文件夹更改为 public_html
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30198669/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to change public folder to public_html in laravel 5
提问by Ravexina
I'm using a shared hosting which uses cPanel as its control panel and within the cPanel public_html
is the default root directory, because of this I can't get my Laravel application work properly.
我正在使用一个共享主机,它使用 cPanel 作为其控制面板,并且 cPanel 内public_html
是默认的根目录,因此我无法让我的 Laravel 应用程序正常工作。
Is there any way to make Laravel use public_html
instead of public folder?
有没有办法让 Laravel 使用public_html
而不是公共文件夹?
回答by Robert
Quite easy to find this with a simple search.
通过简单的搜索很容易找到它。
请参阅:https: //laracasts.com/discuss/channels/general-discussion/where-do-you-set-public-directory-laravel-5
In your index.php add the following 3 lines.
在您的 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__;
});
Edit:
编辑:
As Burak Erdem mentioned, another option (and more preferable) is to put this in the \App\Providers\AppServiceProvider
register()
method.
正如 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');
});
}
回答by Burak Erdem
If Robert's index.php
solution is not working for you, you can also register the following code at Application Service Provider (App\Providers\AppServiceProvider.php)
.
如果 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';
});
}
回答by David
Server
服务器
Methods described in topic are working just fine, so modyfing App\Providers\AppServiceProvider.php register method should do the job:
主题中描述的方法工作正常,因此修改 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';
});
}
Local php artisan serve development
本地php工匠服务开发
However, there is one more issue you can experience. If you're developing your app on local machine and you're using php artisan serve
command to serve your app you're going to break it with above syntax only. You still need to adjust server.php file which exists in main directory. Edit the contents of it and replace each occurance of /public
to /public_html
, so it looks like this:
但是,您还可以遇到另一个问题。如果您在本地机器上开发您的应用程序并且您正在使用php artisan serve
命令来为您的应用程序提供服务,那么您将仅使用上述语法来破坏它。您仍然需要调整主目录中存在的 server.php 文件。编辑它的内容并替换每次出现的/public
to /public_html
,所以它看起来像这样:
<?php
/**
* Laravel - A PHP Framework For Web Artisans
*
* @package Laravel
* @author Taylor Otwell <[email protected]>
*/
$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';
After that. Just stop your server and reload it with php artisan serve
.
在那之后。只需停止您的服务器并使用php artisan serve
.
Front end laravel-mix development
前端laravel-mix开发
If you're using webpack and laravel-mix to generate your css and js files then this also needs some update. Without tweaking webpack.mix.js you will end up with something like this on npm run watch
or npm run production
:
如果你使用 webpack 和 laravel-mix 来生成你的 css 和 js 文件,那么这也需要一些更新。如果不调整 webpack.mix.js,你最终会在npm run watch
or 上得到这样的结果npm run production
:
.
..
public
_html
js
public_html
css
public_html
So it's going to mess up your code. To clarify this you have to provide a public path to your webpack.mix.js file. It could look like this:
所以它会弄乱你的代码。为了澄清这一点,您必须提供 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');
This is going to change the default definition of public directory from public
to public_html
and next lines provides a relative path to your setPublicPath value.
这会将公共目录的默认定义从 to 更改public
为public_html
,下一行提供了 setPublicPath 值的相对路径。
Happy coding.
快乐编码。
回答by T.Arslan
These answers did not work for laravel 5.5, but my own method can help you.
这些答案不适用于 laravel 5.5,但我自己的方法可以帮助您。
Step 1: Discard all files except the public file to the main directory on the server.
步骤1:将除public文件外的所有文件丢弃到服务器上的主目录中。
Step 2: Public file to the public_html file on the server.
第二步:公共文件到服务器上的public_html文件。
Step 3: Pull index.php file from public_html and change it like this.
第 3 步:从 public_html 中拉取 index.php 文件并像这样更改它。
Step 3A:
步骤 3A:
Orginal -> require __DIR__.'/../vendor/autoload.php';
原始 -> require __DIR__.'/../vendor/autoload.php';
Changes -> require __DIR__.'/../**created_folder**/vendor/autoload.php';
变化 -> require __DIR__.'/../**created_folder**/vendor/autoload.php';
Original -> $app = require_once __DIR__.'/../bootstrap/app.php';
原始 -> $app = require_once __DIR__.'/../bootstrap/app.php';
Changes -> $app = require_once __DIR__.'/../**Created_folder**/bootstrap/app.php';
变化 -> $app = require_once __DIR__.'/../**Created_folder**/bootstrap/app.php';
Step 4: Create symlinkcreate.php
第 4 步:创建 symlinkcreate.php
Step 4A: <?php
symlink('/home/**server_directory**/**created_folder**/storage/app/public','/home/**server_directory**/public_html/storage');
步骤 4A: <?php
symlink('/home/**server_directory**/**created_folder**/storage/app/public','/home/**server_directory**/public_html/storage');
Step 4B: yourwebsite.com/symlinkcreate.php visit
步骤 4B:访问 yourwebsite.com/symlinkcreate.php
Step 4C: symlinkcreate.php delete your server.
步骤 4C:symlinkcreate.php 删除您的服务器。
Finally, the directory structure looks like this:
最后,目录结构如下所示:
/etc
/logs
/lscache
/mail
/Your_Created_Folder
../LARAVEL_FOLDERS
/public_html
../css
../js
../.htaccess
../index.php
../web.config
/ssl
/tmp
Finish.
结束。
Laravel 5.5 public_html sorunu i?in bu cevab? g?nül rahatl???yla kullanabilirsiniz.
Laravel 5.5 public_html sorunu i?in bu cevab?g?nül rahatl???yla kullanabilirsiniz。
回答by Ahmadreza
hi guys it works for me ... you can use it
嗨伙计们它对我有用......你可以使用它
go to this address :
转到这个地址:
/app/Providers/AppServiceProvider.php
and append this code to end of file ....
并将此代码附加到文件末尾....
public function register()
{ $this->app->bind('path.public', function() {
return realpath(base_path().'/../public_html');
});
}
回答by Alex
Just want to update all previous answers, if your public_html is not inside laravel folder, then you need to use this code:
只想更新所有以前的答案,如果您的 public_html 不在 laravel 文件夹中,那么您需要使用以下代码:
$this->app->bind('path.public', function() {
return realpath(base_path().'/../public_html');
});
回答by Cameron Hudson
In
bootstrap/app.php
:Add
$app->bind('path.public', function() { return __DIR__; });
right after
$app = new Illuminate\Foundation\Application( realpath(__DIR__) );
Fixing
server.php
:Change
if ($uri !== '/' && file_exists(__DIR__.'/public'.$uri)) { return false; } require_once __DIR__.'/public/index.php';
to
if ($uri !== '/' && file_exists(__DIR__.'/public_html'.$uri)) { return false; } require_once __DIR__.'/public_html/index.php';
In
.gitignore
, change/public/hot /public/storage
to
/public_html/hot /public_html/storage
In
webpack.mix.js
, changemix.js('resources/js/app.js', 'public/js') .sass('resources/sass/app.scss', 'public/css');
to
mix.js('resources/js/app.js', 'public_html/js') .sass('resources/sass/app.scss', 'public_html/css');
在
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');
I'm still looking for a fix for the asset()
function, which remains broken...
我仍在寻找该asset()
功能的修复程序,该功能仍然损坏...
回答by Diego Viniegra
It's not so easy like binding. The cleanest and more elaborated answer is provided by ferrolho here https://laracasts.com/discuss/channels/general-discussion/where-do-you-set-public-directory-laravel-5
不像绑定那么容易。ferrolho 在此处提供了最清晰、更详尽的答案https://laracasts.com/discuss/channels/general-discussion/where-do-you-set-public-directory-laravel-5
But the fastest answer is creating a symbolic link named public pointing to public_html and put your index.php in the last one.
但最快的答案是创建一个名为 public 的符号链接,指向 public_html 并将您的 index.php 放在最后一个。
回答by Mike Rockétt
For those that need to change the public path so that it is also available to Artisan, add the following to bootstrap/app.php
, just after the last singleton
method:
对于那些需要更改公共路径以便 Artisan 也可以使用的人,请将以下内容添加到bootstrap/app.php
,就在最后一个singleton
方法之后:
$app->bind('path.public', function () {
return base_path("<your public directory name>");
});
回答by Arthur Tarasov
For me none of this worked until I changed the root public folder in server settings. In production it is in /etc/nginx/sites-enabled/<your_site_name>
. Edit it with a text editor and just scroll until you see root path pointing to /public
folder. Change that to whatever is your new public folder and restart the server. On local, I had to change path in Homestead.yaml
and /etc/nginx/sites-enabled/<your_site_name>
after I SSHed into Vagrant. vagrant reload --provision
to make sure it caught on. Then I also edited index.php
and registered a new path in the service provider just in case, but it seems to work without it. I don't use any 3rd party dependencies with Laravel though so I don't know if this will work in that case.
对我来说,在我更改服务器设置中的根公用文件夹之前,这些都不起作用。在生产中,它在/etc/nginx/sites-enabled/<your_site_name>
. 使用文本编辑器编辑它,然后滚动直到看到指向/public
文件夹的根路径。将其更改为您的新公用文件夹并重新启动服务器。在当地,我不得不在变化路径Homestead.yaml
和/etc/nginx/sites-enabled/<your_site_name>
后我SSHed成流浪汉。vagrant reload --provision
以确保它流行起来。然后我还在index.php
服务提供者中编辑并注册了一个新路径以防万一,但它似乎没有它。我不使用 Laravel 的任何 3rd 方依赖项,所以我不知道在这种情况下这是否有效。