如何配置Laravel 5.1路由


How to configure Laravel 5.1 routing?

我已经通过composer安装了一个新的laravel 5.1。默认的预定义路由如下所示:

Route::get('/', function () {
    return view('welcome');
});

按照Laravel 5.1的文档进行安装和基本配置后,它可以很好地使用上述路由。但是当我把它改成:

Route::get('test', function () {
    return view('welcome');
});   
or 
Route::get('/test', function () {
    return view('welcome');
});   

并试图通过:

访问同一页面
http://localhost/mylaravel/public/test

我得到了以下错误。

Not Found
The requested URL /mylaravel/public/test was not found on this server.

似乎路由不起作用。我已按如下命令启用mod_rewrite

a2enmod rewrite
service apache2 restart

我已经尝试了三种不同的.htaccess如下。

# the defualt one
<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>
    RewriteEngine On
    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)/$ /$1 [L,R=301]
    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>
# attempt two
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
# attempt three
<IfModule mod_rewrite.c>
     RewriteEngine On
     RewriteBase /
     RewriteCond %{REQUEST_FILENAME} !-f
     RewriteCond %{REQUEST_FILENAME} !-d
     RewriteRule . /index.php [L]
</IfModule>

它根本不起作用。我忘记配置什么了?如何解决这个路由问题?

在这里的一些家伙评论的帮助下,我已经解决了这个问题,我做了下面的事情。

sudo a2enmod rewrite
sudo service apache2 restart
sudo nano /etc/apache2/sites-available/000-default.conf

找到DocumentRoot /var/www/html并在下面添加以下行:

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

用下面的命令重新启动apache。

sudo service apache2 restart

使用Laravel安装提供的默认.htaccess,它可以工作。

问题是您的本地服务器正在运行。通过在控制台中运行以下命令启动服务器:

php artisan serve --host=localhost --port=8080

,然后尝试通过这个地址访问你的应用程序:

http://localhost:8080/test

在laravel应用程序中,所有请求都由index.php文件接收,当你请求诸如
之类的东西时http://localhost/mylaravel/public/test
那么服务器认为您请求的是静态文件