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


当前回答

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

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

其他回答

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

我用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 5 url删除公共。 你只需要从公共目录中剪切index.php和.htaccess,并将其粘贴到根目录,仅此而已 将index.php中的两行替换为

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

注意:以上方法仅适用于初学者,因为他们在设置虚拟主机时可能会遇到问题,最好的解决方案是在本地机器上设置虚拟主机并将其指向项目的公共目录。

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

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

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

添加。htaccess文件到你的根文件夹,粘贴下面的代码,用你自己的域名替换yourdomainname

RewriteEngine上 重写HTTP_HOST} ^(www.) ^/public/ .重写%{REQUEST_URI}

RewriteCond %{REQUEST_FILENAME} 重写%{REQUEST_FILENAME} !-d

RewriteRule ^(.*)$ /public/$1

重写HTTP_HOST} ^(www.) RewriteRule ^ (/) ?$ /public/index.php