【问题标题】:Why is my route in Laravel returning a 404 error?为什么我在 Laravel 中的路由返回 404 错误?
【发布时间】:2016-12-08 13:27:06
【问题描述】:

所以在我的 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代码吗?也许其中的所有路线?这可能是由于在您遇到错误的路由顶部有带有前缀的路由组
  • 尝试在您的联系方式前面添加一个“/”...Route::get('/contact', function....
  • @Nash 请试试这个命令php artisan routesphp artisan route:list 看看你的路线是否在那里!
  • php artisan route:list 如果你使用的是 laravel 版本 >5
  • 这真的很奇怪!请尝试清除缓存artisan route:clear谁知道!

标签: laravel laravel-5


【解决方案1】:

好的,我解决了我的问题。如果其他人遇到此问题,请确保您的服务器上启用了 mod_rewrite。您可以通过转到终端并输入来完成此操作

a2enmod rewrite

然后输入

service apache2 restart

它现在就像一个魅力。

【讨论】:

    猜你喜欢
    • 2018-12-11
    • 2019-04-20
    • 1970-01-01
    • 1970-01-01
    • 2021-12-16
    • 2021-02-21
    • 2021-01-31
    • 1970-01-01
    • 2018-01-20
    相关资源
    最近更新 更多