【问题标题】:Laravel 4 : NotFoundHttpExceptionLaravel 4:NotFoundHttpException
【发布时间】:2013-08-17 01:38:49
【问题描述】:

我真的是 Laravel 的新手,我正在尝试让一个表单正常工作。

所以我有一个页面(管理员/索引),其中只有一个表单,其中包含映射到 AdminController@test 的路由。表单提交正常,但随后我收到 NotFoundHttpException。 :(

index.blade.php 中表单构建器的代码是:

@extends('layouts.master')

@section('title')
Admin
@stop

@section('content')
{{ Form::open(array('route' => 'test', 'method' => 'get')) }} <!-- Works with AdminController@index -->
    {{ Form::text('info') }}
{{ Form::close() }}
@stop

有问题的路线是:

    Route::get('/admin/test/' , array( 'as' => 'test' , 
                                       'uses' => 'AdminController@test'));

而有问题的控制器是:

class AdminController extends BaseController{


    public function index(){
        return View::make('admin.index');
    }

    public function test(){
        error_log('Yay!');
    }

}

就像我说的,在 admin/index 上的简单表单提交,但它没有提交给控制器,只是提交给 NotFoundHttpException。

编辑: 表单的 HTML 如下所示:

<form method="GET" action="http://localhost/showknowledge/admin/test/" 
accept-charset="UTF-8">   
 <input name="info" type="text">
</form>

【问题讨论】:

  • 您是否尝试过将表单直接指向控制器上的操作? {{ Form::open(array('action' =&gt; 'AdminController@test', 'method' =&gt; 'get')) }}
  • 是的,刚刚尝试过同样的问题。 :(
  • 哦,等一下,一旦表单被提交,在请求的过程中会发生什么?它会将Yay 写入日志,对吗?
  • 表单的 HTML 在页面上是什么样子的?上面可以加吗?
  • 啊好的,你能这样声明你的路线吗? Route::get('admin/test/' , array( 'as' =&gt; 'test' , 'uses' =&gt; 'AdminController@test'));

标签: exception laravel laravel-4 laravel-routing


【解决方案1】:

将您的路由逻辑移动到AdminController 并使用RESTful controller 可能更清楚:

routes.php 中添加这个,并删除/admin/index/admin/test 的两个路由定义:

Route::controller('admin' , 'AdminController');

这会将所有对 admin/ 的请求定向到您的 AdminController。现在您需要重命名您的函数以包含 HTTP 动词(GET、POST 或任何)和路由的下一个组件:

public function getIndex()  // for GET requests to admin/index
{ 
    //blha blah blah 
}

public function getTest()  // for GET requests to admin/test
{ 
    //blha blah blah 
}

最后,更新您的表单以直接通过 action 关键字使用该路由:

{{ Form::open(array('action' => 'AdminController@getTest', 'method' => 'get')) }}

注意,使用 missingMethod() 来捕获未处理的请求也非常有用,更多信息请参阅 Laravel 文档:http://laravel.com/docs/controllers#handling-missing-methods

希望有帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-28
    • 2013-10-13
    • 1970-01-01
    • 2016-12-02
    • 1970-01-01
    相关资源
    最近更新 更多