我知道这是一个非常流行的问题,但我还没有找到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


当前回答

我用2个答案解决了这个问题:

Renaming the server.php to index.php (no modifications) Copy the .htaccess from public folder to root folder (like rimon.ekjon said below) Changing .htaccess it a bit as follows for statics: 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 ^ index.php [L] RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_URI} !^/public/ RewriteRule ^(css|js|images)/(.*)$ public/$1/$2 [L,NC] If there are any other static files needed just add the extension to the previous declared list

其他回答

可以在laravel5中删除公共url。遵循以下步骤:

步骤1。复制所有文件从公共和粘贴根目录

步骤2。打开index.php文件 替换为

 require __DIR__.'/../bootstrap/autoload.php';

to

 require __DIR__.'/bootstrap/autoload.php';

and

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

to

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

并删除所有缓存和cookie。

我用2个答案解决了这个问题:

Renaming the server.php to index.php (no modifications) Copy the .htaccess from public folder to root folder (like rimon.ekjon said below) Changing .htaccess it a bit as follows for statics: 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 ^ index.php [L] RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_URI} !^/public/ RewriteRule ^(css|js|images)/(.*)$ public/$1/$2 [L,NC] If there are any other static files needed just add the extension to the previous declared list

在Laravel设置中总是有一个公共文件夹的原因,所有与公共相关的东西都应该出现在公共文件夹中,

不要将你的ip地址/域名指向Laravel的根文件夹,而是指向公共文件夹。将服务器Ip指向根文件夹是不安全的。因为除非你在.htaccess中写了限制,否则一个文件可以很容易地访问其他文件。

只要在.htaccess文件中写入重写条件,并安装重写模块并启用重写模块,就可以解决路由中添加public的问题。

对于拉拉维尔 5:

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

就是这样!

在Laravel 5.5中,在根目录中创建。htess文件,并放置以下代码

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

    RewriteEngine On

    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>