我知道这是一个非常流行的问题,但我还没有找到Laravel 5的有效解决方案。很长一段时间以来,我一直试图从Codeigniter迁移,但这个复杂的安装过程一直让我望而却步。

我不想运行一个虚拟机,这只是看起来尴尬时切换项目。

我不想将我的文档根文件夹设置为公共文件夹,这在项目之间切换时也很尴尬。

我已经尝试了.htaccess mod_rewrite方法

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

这只是给了我一个Laravel NotFoundHttpException在compiled.php行7610。

当我前段时间尝试L4时,我使用了将公共文件夹的内容移动到根目录的方法。L5的结构完全不同,遵循相同的步骤完全破坏了Laravel(服务器只会返回一个空白页面)。

在开发环境中是否有一种合适的方法来删除“public”:

与L5一起工作 允许我轻松地在项目之间切换(我通常同时工作2或3个项目)。

谢谢

**我使用MAMP和PHP 5.6.2


当前回答

以下是截至2018年5月对我有效的最佳和最短的解决方案 Laravel 5.5

just cut your .htaccess file from the /public directory to the root directory and replace it content with the following code: <IfModule mod_rewrite.c> <IfModule mod_negotiation.c> Options -MultiViews </IfModule> RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)/$ /$1 [L,R=301] RewriteCond %{REQUEST_URI} !(\.css|\.js|\.png|\.jpg|\.gif|robots\.txt)$ [NC] RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^ server.php [L] RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_URI} !^/public/ RewriteRule ^(css|js|images)/(.*)$ public/$1/$2 [L,NC] </IfModule> Just save the .htaccess file and that is all. Rename your server.php file to index.php. that is all enjoy!

其他回答



请注意,当使用Docker服务Laravel项目时:你不需要做这些。只有当你的网站的根目录(或更常见的:public_html)是你的Laravel项目时才使用这个选项(当你使用Docker时不是这样)。

不!

你真的不应该把Laravel根文件夹中的server.php重命名为index.php 然后从/public目录复制。htaccess文件到你的Laravel根文件夹!!

这样,每个人都可以访问您的一些文件。例如环境)。你自己试试。你不会想要的!


DO

相反,你应该在根目录下创建一个。htaccess文件,如下所示:

RewriteEngine On
RewriteCond %{REQUEST_URI} !^/public/
RewriteRule ^(.*)$ /public/$1 [L,QSA]

这将无声地将所有基本uri重写到/public文件夹。甚至所有的头文件,例如HTTP授权头文件,以及所有可选的URI参数都将被默默地传递到/public文件夹。

这是所有

我以前读过一些文章,它工作得很好,但真的不知道是否安全

    a. Create new folder local.
    b. Move all project into the local folder expect public folder.
    c. Move all the content of public folder to project root.
    d. Delete the blank public folder
    f. Edit the index file. 

编辑index.php

require __DIR__.'/../bootstrap/autoload.php';
$app = require_once __DIR__.'/../bootstrap/app.php';

to

require __DIR__.'/local/bootstrap/autoload.php';
$app = require_once __DIR__.'/local/bootstrap/app.php';

首先,您可以使用这些步骤

Laravel 5: 1. 将Laravel根文件夹中的server.php重命名为index.php 2. 从/public目录复制.htaccess文件到你的Laravel根文件夹。

来源:https://stackoverflow.com/a/28735930

在你遵循这些步骤之后,你需要改变所有的CSS和脚本路径,但这将是累人的。

解决方案:简单地你可以对helpers::asset函数做一些小的修改。

:

开放供应商\ laravel \ \ src \照亮\ \ helpers.php基础框架 后藤第130行 写“公共/”。用$path代替$path, 函数资产($path, $secure = null){ 返回应用程序(url) - >资产(“公共/”。路径,确保美元); }

我知道这个问题有很多解决方案。最好和最简单的解决方案是在根目录中添加.htaccess文件。

我尝试了我们为这个问题提供的答案,但我在Laravel中遇到了一些auth:api守卫的问题。

以下是最新的解决方案:

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>

    RewriteEngine On
    RewriteCond %{HTTP:Authorization} ^(.*)
    RewriteRule .* - [e=HTTP_AUTHORIZATION:%1]

    RewriteCond %{REQUEST_FILENAME} -d [OR]
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule ^ ^$1 [N]

    RewriteCond %{REQUEST_URI} (\.\w+$) [NC]
    RewriteRule ^(.*)$ public/$1

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ server.php
</IfModule>

在根目录中创建.htaccess文件并添加以下代码。一切都很顺利

我使用的另一种方法是在htdocs或www中使用ln命令创建一个符号链接(在Linux中,不知道Win),即ln projectname/public project 因此,站点可以通过localhost/project访问