laravel 5 中的 auth 中间件拦截了表单提交

Form submit intercepted by auth middleware in laravel 5

我一直在从事 laravel 5.7 博客项目。我想评论一篇文章。

我需要实现这个:

我的评论管理员在这里:

public function __construct()
{
    $this->middleware('auth');
}

public function store(Post $post) {
    $comment = Comment::create([
        'body' => session('comment')?:request('body'),
        'post_id'  => $post->id,
        'user_id' => auth()->user()->id 
    //before logging in, you don't have an user_id yet.
    ]);        
    return back()->with('success', 'Add comment succeeded');
}

web.php路线在这里:

Route::post('/posts/{post}/comments', 'CommentsController@store')->name('addComment');

基本上 auth 中间件拦截了我提交的表单数据,我想通过 auth 中间件处理我的表单数据。登录后不丢失。

我认为您的问题的解决方案在这里: https://laravel.com/docs/5.7/session#storing-data

这是 solution.A 小 tricky.Save 对会话的评论,然后再进行身份验证 middleware.After 登录,获取创建评论的路径。 路线:

Route::get('/posts/{post}/comments', 'CommentsController@store')->name('addComment');
Route::post('/posts/{post}/comments', 'CommentsController@commentSave');

评论管理员:

public function __construct()
{
    $this->middleware('auth', ['except' => ['commentSave']]);
}
public function commentSave(Request $request){
    $url = \URL::previous();
    session(['comment' => $request->input('body')]);
    return redirect("$url/comments");
}  

public function store(Post $post){
    if(session('comment')){
        $comment = Comment::create([
            'body' => session('comment'),
            'post_id'  => $post->id,
            'user_id' => auth()->user()->id
        ]);
        session(['comment' => null]);
        return redirect("posts/$post->id")->with('success', 'Add comment succeeded');
    }
    return redirect("posts/$post->id");         
}