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


当前回答

在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>

其他回答

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

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) - >资产(“公共/”。路径,确保美元); }

我想添加到@Humble Learner,并注意到“修复”资产的url路径的正确位置是/Illuminate/Routing/UrlGenerator.php/asset()。

更新方法以匹配:

public function asset($path, $secure = null)
{
    if ($this->isValidUrl($path)) return $path;
    $root = $this->getRootUrl($this->getScheme($secure));
    return $this->removeIndex($root).'/public/'.trim($path, '/');
}

这将修复脚本,样式和图像路径。任何资源路径。

1)我还没有找到L5中公共目录移动的工作方法。虽然可以修改bootstrap index.php中的一些内容,但似乎有几个辅助函数是基于公共目录存在的假设。总之,老实说,你真的不应该移动公共目录。

2)如果你使用MAMP,那么你应该为每个项目创建新的vhosts,每个服务于项目的公共目录。创建后,您可以通过定义的服务器名访问每个项目,如下所示:

http://project1.dev
http://project2.dev

即使我不建议把Laravel放在根文件夹上,但在某些情况下它是不可避免的; 对于这些情况下,上述方法并不适用于资产,所以我做了一个快速修复改变htaccess: 将server.php复制到index.php后,编辑.htaccess文件,如下所示:

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

    RewriteEngine On

    ### fix file rewrites on root path ###
    #select file url
    RewriteCond %{REQUEST_URI} ^(.*)$
    #if file exists in /public/<filename>
    RewriteCond %{DOCUMENT_ROOT}/public/$1 -f
    #redirect to /public/<filename>
    RewriteRule ^(.*)$ public/$1 [L]
    ###############

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)/$ /$1 [L,R=301]

    # Handle Front Controller...

    #RewriteCond %{REQUEST_FILENAME} -d # comment this rules or the user will read non-public file and folders!
    #RewriteCond %{REQUEST_FILENAME} -f #
    RewriteRule ^ index.php [L]
</IfModule>

这是一个快速修复我必须使任何人都欢迎升级它。

请将这些行添加到您的根.htaccess文件中,它隐藏了/public并重定向到所有没有public的url

RewriteEngine on
RewriteCond %{REQUEST_URI} !public/
RewriteRule (.*) /public/$1 [L]