为什么我的Laravel路由返回404错误?

4

所以,在我的routes.php文件中,我有这个:

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

当我访问domain.com/contact时,出现返回错误。然而当我输入以下内容:
Route::get('/', function() {
    return view('contact');
});

当我访问domain.com时,页面出现了。有什么想法是什么原因造成的吗?

完整路由文件:

<?php

/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the controller to call when that URI is requested.
|
*/

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

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

php artisan route:list的返回结果:

+--------+----------+---------+------+---------+------------+
| Domain | Method   | URI     | Name | Action  | Middleware |
+--------+----------+---------+------+---------+------------+
|        | GET|HEAD | /       |      | Closure | web        |
|        | GET|HEAD | contact |      | Closure | web        |
+--------+----------+---------+------+---------+------------+

你能分享一下你的routes.php文件中更多的代码吗?也许包括其中所有的路由?这可能是因为你在路由上方有一个带前缀的路由组。 - Tommy Lee
1
尝试在您的联系路由前面添加“/”... Route :: get('/ contact',function .... - Tommy Lee
1
@Nash,请尝试执行以下命令 php artisan routesphp artisan route:list,看看你的路由是否存在! - Ismail RBOUH
1
如果您使用Laravel版本>5,则返回已翻译的程序相关内容:“php artisan route:list”。 - Tommy Lee
3
真是很奇怪!请尝试清除缓存artisan route:clear,说不定会有帮助! - Ismail RBOUH
显示剩余14条评论
1个回答

4

好的,我解决了我的问题。如果其他人也遇到了这个问题,请确保您的服务器启用了mod_rewrite模块。您可以通过进入终端并输入以下命令来完成:

a2enmod rewrite

然后输入以下命令:

service apache2 restart

它现在可以完美地工作了。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接