如何在 Laravel 中以 root 身份创建公共文件夹?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40756665/
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 make public folder as root in Laravel?
提问by Martin AJ
I use Laravel framework. As you know, its directory looks like this:
我使用 Laravel 框架。如您所知,它的目录如下所示:
To open the homepage of my website (Route::get('/', 'HomeController@index');) I need to open /publicfolder of directory. In other word, to see the first page of my website, here is the URL:
要打开我网站的主页 ( Route::get('/', 'HomeController@index');) 我需要打开/public目录的文件夹。换句话说,要查看我网站的第一页,请使用以下 URL:
http://example.com/public
anyway, onlymy domainname (http://example.com/) isn't my root. How can I make /publicfolder as root?
无论如何,只有我的域名 ( http://example.com/) 不是我的根。如何/public以root身份创建文件夹?
Curently I've found a workaround. I can create an index.phpon the root and write a redirect code to /publicfolder. So when user enters http://example.com/, it will be redirected to http://example.com/publicautomatically. But still that's ugly. I don't like to see /publicin the URL. Any suggestion?
目前我找到了一个解决方法。我可以index.php在根目录上创建一个并将重定向代码写入/public文件夹。所以当用户输入时http://example.com/,它会被http://example.com/public自动重定向到。但这仍然很丑陋。我不喜欢/public在 URL 中看到。有什么建议吗?
回答by Chonchol Mahmud
There are many ways to do this.
有很多方法可以做到这一点。
You just need to cut index.phpand .htaccess from publicdirectory and paste it in the root directory, that's all and replace two lines in index.phpas
您只需index.php要从public目录中剪切和 .htaccess并将其粘贴到根目录中,就这样,并将其中的两行替换index.php为
require __DIR__.'/bootstrap/autoload.php';
$app = require_once __DIR__.'/bootstrap/app.php';
Best way to do is .htaccess. Create .htaccessfile in root directory in Laravel installation. And the below code will work for this.
最好的办法是.htaccess。.htaccess在 Laravel 安装的根目录中创建文件。下面的代码将适用于此。
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ public/ [L]
</IfModule>
You can read from here: WAY TO DO
您可以从这里阅读:WAY TO DO
回答by Alexey Mezenin
Do not mofidy any Laravel files. Use web server (Apache or Nginx) to point Laravel project to publicdirectory.
不要修改任何 Laravel 文件。使用 Web 服务器(Apache 或 Nginx)将Laravel 项目指向public目录。
For Apache you can use these directives:
对于 Apache,您可以使用这些指令:
DocumentRoot "/path_to_laravel_project/public"
<Directory "/path_to_laravel_project/public">
For nginx, you should change this line:
对于 nginx,您应该更改此行:
root /path_to_laravel_project/public;
回答by Piyush
Step 1: Put your /public/index.phpand /public/htaccessfile to your root directory as /index.phpand /htaccess.
Step 2: Now Make changes in your index.php
第 1 步:将您的/public/index.php和/public/htaccess文件作为/index.php和/htaccess放在您的根目录中。
第 2 步:现在在 index.php 中进行更改
require __DIR__.'/../bootstrap/autoload.php'; //OLD code
$app = require_once __DIR__.'/../bootstrap/app.php';
to
到
require __DIR__.'./bootstrap/autoload.php'; //NEW code
$app = require_once __DIR__.'./bootstrap/app.php';


