在laravel中传递数据NotFoundHttpException

时间:2017-11-30 11:09:33

标签: php laravel

我有这个观点:

<div class="window-options">
    <form class="form-horizontal" method="POST" action="/api/b/sendInsert/{{$id}}"> 
        <label for="inputEmail" class="col-lg-2 conrol label">Comment</label>
        <input type="text" name="inputEmail" class="form-control" id="inputEmail" placeholder="Comment">
        <button type="submit" class="btn btn-primary">Submit</button>
    </form>
</div>

我的路线:

Route::post('b/sendInsert/{$id}, 
Mejili\Core\Controllers\BoardController@insert');

我的控制器:

public function insert($id)
{
    $data['id'] = $id;
    $user = Auth::user()->id;
    $comment = Input::get('inputEmail');
    $comments=new CardComment;
    $comments->commenter_id = $user;
    $comments->card_id = $id;
    $comments->save();
    return Redirect::route('board', $data);
}

抛出NotFoundHttpException。有人能帮我吗?提前谢谢。

3 个答案:

答案 0 :(得分:0)

表单的操作路径和路径不匹配

action="/api/b/sendInsert/{{$id}}" change route to 
Route::post('api/b/sendInsert/{$id}', 'Mejili\Core\Controllers\BoardController@insert');

Or you can use url() function in action attribute

答案 1 :(得分:0)

Laravel忽略了您的动态路由uri $id,因为编译器只接受\w个字符串。它在Illuminate\Routing\RouteCompiler类中定义。因此,删除$会使其像往常一样被识别。

Route::post('b/sendInsert/{id}', 'Mejili\Core\Controllers\BoardController@insert');

答案 2 :(得分:0)

您可能需要考虑为路由提供别名,而不是使用普通路由。

Route::get('sample_route',['as' => "sampleroute",'uses' => "SomeController@somemethood"]);
Route::post('sample_route',['as' => "post_sampleroute",'uses' => "SomeController@submitmethod"]);

上面给出的路由在不同的HTTP协议中位于相同的URL中,您只需在表单中使用可能如下所示的路由。

<form method="POST" action="{{route('post_sampleroute')}}">
...some inputs here.
</form>

您的路线的良好命名惯例可能对您的项目有很大帮助。 :)

$this->group(['as' => "user.", 'prefix' => "user"], function() {
            $this->get('/', ['as' => "index", 'uses' => "UserController@index"]);
            $this->get('create', ['as' => "create", 'uses' => "UserController@create"]);
            $this->post('create', ['as' => "store", 'uses' => "UserController@store"]);
            $this->get('edit/{id?}', ['as' => "edit", 'uses' => "UserController@edit"]);
            $this->post('edit/{id?}', ['as' => "update", 'uses' => "UserController@update"]);
            $this->get('trash', ['as' => "trash", 'uses' => "UserController@trash"]);
            $this->any('restore/{id?}', ['as' => "restore", 'uses' => "UserController@restore"]);
            $this->any('delete/{id?}', ['as' => "destroy", 'uses' => "UserController@destroy"]);
        });