【问题标题】:Do not understand why I keep getting this error: Symfony \ Component \ HttpKernel \ Exception \ NotFoundHttpException不明白为什么我不断收到此错误:Symfony \ Component \ HttpKernel \ Exception \ NotFoundHttpException
【发布时间】:2014-11-11 14:07:02
【问题描述】:

我知道这是一个路由错误,但我在我的路由中找不到任何错误。

  // comments
  Route::get('/comments', 'CommentsController@index');

这是控制器。

 /**
 * Display a listing of the resource.
 * GET /comments
 *
 * @return Response
 */
public function index()
{
    return View::make('comments.create');
}

提前谢谢你。对某人来说,这可能是一个简单的 15 分。

【问题讨论】:

  • 你在尝试什么网址?你的虚拟主机设置是什么?你的 .htaccess 中有什么? (假设您使用的是 apache)
  • desk.dev:8000/cmets
  • 你的控制器叫什么名字?类的名称应为 CommentsController,文件的名称应为 CommentsController.php
  • 我知道 CommentsController 类扩展了 BaseController
  • 您可以发布完整的routes.php 文件吗?

标签: php symfony laravel laravel-4 routing


【解决方案1】:

我认为当您尝试提交表单时会出现问题。使用时:

Route::get('/comments', 'CommentsController@index');

它仅适用于GET 请求,如果您尝试提交表单,您可能使用POST 方法

您可以添加到您的路线:

Route::post('/comments', 'CommentsController@index');

如果您想路由到控制器中的相同方法或创建另一个方法并路由到它。

你也可以使用:

Route::any('/comments', 'CommentsController@index');

如果您不关心方法 - 所有请求(包括 POST 和 GET)都将被定向到路由。

【讨论】:

    【解决方案2】:

    鉴于显示了完整的 routes.php 文件,您需要在顶部添加 PHP 左括号:

    <?php
    
    // comments
    Route::get('/comments', 'CommentsController@index');
    

    省略它会给你准确的错误。

    【讨论】:

    • 那么这意味着你的routes.php文件没有被加载。您使用的是默认设置吗?
    【解决方案3】:

    我能想到两件事。第一条是你的路线:

    Route::get('/comments', 'CommentsController@index');
    

    我认为可能是:

    Route::get('comments', 'CommentsController@index'); // Note the omitted /
    Route::get('comments', array('as' => 'comments', 'uses' => 'CommentsController@index');
    

    理论上,这应该可以解决问题,但如果没有,我在使用视图时还有另外一件事要做。你有:

    return View::make('comments.create');
    

    我用:

    return View::make('comments/create');
    

    文件夹结构的位置:

    views->comments->create.blade.php
    

    现在我不知道这是否/如何影响它,试试看吧。

    【讨论】:

    • 谢谢,但这并没有解决问题。
    • 嗯。很奇怪……现在你只有一条路线吗?比如你有 `Route::get("/", "HomeController@showWelcome") 吗?
    • 我确实找到了这些 Route::get('/', 'AuthController@index'); Route::get('/login', 'AuthController@login'); Route::post('/login', ['before' => 'csrf', 'uses' => 'AuthController@authenticate']); Route::get('/logout', 'AuthController@logout');
    • 我试图通过向我的现有项目添加 CommentsController、新 Route 和新视图来复制您的错误,但它确实有效。 Tbh,我不确定发生了什么。抱歉,我帮不上忙,祝你好运!
    【解决方案4】:

    您在这条路线上似乎有一个auth 过滤器。如果您的AuthController 未设置,或者缺少login 方法,当filter.php 中的auth 过滤器尝试重定向到您的登录页面时,您将获得NotFoundHttpException

    【讨论】:

      猜你喜欢
      • 2021-05-28
      • 1970-01-01
      • 2021-04-28
      • 1970-01-01
      • 2014-06-28
      • 2014-08-13
      • 2016-05-21
      相关资源
      最近更新 更多