laravel 5 中的 auth 中间件拦截了表单提交
Form submit intercepted by auth middleware in laravel 5
我一直在从事 laravel 5.7 博客项目。我想评论一篇文章。
我需要实现这个:
- 登录前,我可以在评论文本区输入任何内容
- 我提交评论(当然会被auth中间件拦截)
- 然后我被重定向到登录页面
- 登录后,我希望我的应用程序能够自动提交以前的表单数据(或评论),而不是再次输入相同的评论
- 我认为这是当今许多网站中非常常见的业务逻辑,但我应该如何实现这一点?
我的评论管理员在这里:
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");
}
我一直在从事 laravel 5.7 博客项目。我想评论一篇文章。
我需要实现这个:
- 登录前,我可以在评论文本区输入任何内容
- 我提交评论(当然会被auth中间件拦截)
- 然后我被重定向到登录页面
- 登录后,我希望我的应用程序能够自动提交以前的表单数据(或评论),而不是再次输入相同的评论
- 我认为这是当今许多网站中非常常见的业务逻辑,但我应该如何实现这一点?
我的评论管理员在这里:
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");
}