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


当前回答

对于XAMPP用户来说,要在不接触laravel默认文件系统的情况下从url中删除public,需要为应用程序设置一个虚拟主机,只需遵循以下步骤即可

打开XAMPP控制面板应用程序并停止Apache。请注意,后期的Windows机器可能会将其作为服务运行,因此请勾选Apache模块左侧的复选框。 进入C:/xampp/apache/conf/extra或xampp文件所在目录。 使用文本编辑器打开名为httpd-vhosts.conf的文件。 在第19行左右找到# NameVirtualHost *:80并取消注释或删除散列。 在文件的最底部粘贴以下代码:

<VirtualHost *>
    ServerAdmin admin@localhost.com
    DocumentRoot "C:/xampp/htdocs" # change this line with your htdocs folder
    ServerName localhost
    ServerAlias localhost
    <Directory "C:/xampp/htdocs">
        Options Indexes FollowSymLinks Includes ExecCGI
        Order allow,deny
        Allow from all
    </Directory>
</VirtualHost>

现在您可以复制并粘贴下面的代码来添加您的虚拟主机目录。例如,我正在一个名为Eatery Engine的网站上工作,所以下面的代码片段将允许我在我的本地安装上使用子域:

<VirtualHost eateryengine.dev>
    ServerAdmin admin@localhost.com
    DocumentRoot "C:/xampp/htdocs/eateryengine" # change this line with your htdocs folder
    ServerName eateryengine.dev
    ServerAlias eateryengine.dev
    <Directory "C:/xampp/htdocs/eateryengine">
        Order allow,deny
        Allow from all
    </Directory>
</VirtualHost>

接下来转到你的Windows主机文件编辑你的HOSTS。该文件将位于C:/Windows/System32/drivers/etc/hosts,其中hosts是文件。用记事本打开。 寻找在DNS本身中处理的#localhost名称解析。

127.0.0.1 localhost

本地主机名解析在DNS本身中处理。

127.0.0.1       localhost
127.0.0.1       eateryengine.dev #change to match your Virtual Host.
127.0.0.1       demo.eateryengine.dev #manually add new sub-domains.

重新启动Apache并测试所有内容。

原文可以在这里找到

其他回答

我用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。

添加。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

您可以使用各种方法从url中删除公共关键字。

1)如果你使用专用主机,你有根访问权限,那么你可以使用虚拟主机从url中删除公共关键字。你应该给DocumentRoot路径加上public。这会启动index from public directory并从url中移除它。

<VirtualHost *:80>
    ServerAdmin info@example.com
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/html/{yoursourcedirectory}/public

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

2)如果你没有你的主机的根访问权限,那么你应该在你的根目录下生成一个新的。htaccess文件,并把下面的代码

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

你可以在这里得到更多的参考资料。

你不需要做太多,只需要在根目录上创建一个.htaccess文件,然后粘贴/保存下面的文件;

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