Laravel添加邮政路线到资源路线

时间:2016-04-22 15:43:46

标签: php laravel laravel-5.2

我有一个使用资源路由的Laravel 5.2应用程序。我有一个如下:

Route::resource('submissions', 'SubmissionsController');

我想在索引页面上为它添加一个新的Post路由以获取排序表单。

Route::post('submissions', [
    'as' => 'submissions.index',
    'uses' => 'SubmissionsController@index'
]);

我已经在我的routes.php。

中将Post路线放在我的资源路线上方

但是,在我的新邮政路线上正在执行名为SubmissionRequest的验证请求,该验证请求用于提交资源中的表单。这是我的SubmissionsController方法。

public function index(SortRequest $req)
{
    $submission = new Submission;
    $submission = $submission->join('mcd_forms', 'mcd_forms.submission_id', '=', 'submissions.id')->where('user_id', Auth::user()->id);

    $data['sort_types'] = [
        'name' => 'Name',
        'form_type' => 'Type'
    ];

    $data['direction'] = ( !empty($req['asc']) ? 'asc' : 'desc' );
    $data['dataVal'] = ( !empty($req['sort_type']) ? $req['sort_type'] : 'submissions.id' );

    $submission->whereNull('submissions.deleted_at')->orderBy(
        $data['dataVal'],
        $data['direction']
    );

    $data['submissions'] = $submission->get();

    return view('submissions.index')->with($data);
}

因此,当我从索引页面提交排序表单时,它正在运行SubmissionRequest验证,即使我专门调用SortRequest验证。我做错了什么?

2 个答案:

答案 0 :(得分:2)

我解决了。

由于我的Post路由与Get的{​​{1}}路由冲突,我在submissions.index路由下添加了以下内容:

Resource

这允许路由通过覆盖自动生成的请求来接受Route::match(['get', 'post'], 'submissions', [ 'as' => 'submissions.index', 'uses' => 'SubmissionsController@index' ]); Get请求。

文档在这里:https://laravel.com/docs/master/routing#basic-routing

答案 1 :(得分:0)

Route::match(['get', 'post'], 'submissions', [
    'as' => 'submissions.index',
    'uses' => 'SubmissionsController@index'
]);

在laravel 5中,它与@store操作冲突