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


当前回答

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

    RewriteEngine On

    # Handle Authorization Header
    RewriteRule ^(.*)$ public/$1 [L]
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

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

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>

激活mod_rewrite模块

sudo a2enmod rewrite

重新启动apache

sudo service apache2 restart

要在.htaccess文件中使用mod_rewrite(这是一个非常常见的用例),请使用

sudo nano /etc/apache2/sites-available/000-default.conf

在“DocumentRoot /var/www/html”下面添加以下行:

<Directory "/var/www/html">
AllowOverride All
</Directory>

重新启动服务器:

sudo service apache2 restart

其他回答

以下是截至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!

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

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, '/');
}

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

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

    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';

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

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

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