[英]Laravel 5.4: How do I redirect back to correct page when I have 3 post pages using the same url?
I want to keep the same url across 3 pages that have POST forms. 我想在具有POST表单的3个页面上保持相同的URL。 When a form fails validation i'm doing
\\Redirect::back()->withErrors()
but if i'm on the second page it's returning to the 1st page. 当表单验证失败时,我正在执行
\\Redirect::back()->withErrors()
但是如果我在第二页上,它将返回第一页。 How can I return to the 2nd page instead of the 1st? 如何返回第二页而不是第一页?
My routes are: 我的路线是:
Route::post("/test",['as'=>'test','uses'=>'TestController@post']);
Route::get("/test",function(){ return View::make("pages.test"); });
My controller is: 我的控制器是:
class TestController extends Controller
{
function post(Request $request){
if($request->has("test1")){
return view('/pages/test2');
}elseif($request->has("test2")){
return \Redirect::back()->withErrors(["my error"]);
}else{
return "unknown test";
}
}
}
My blade files are test.blade.php: 我的刀片文件是test.blade.php:
{{ Form::open(['action' => ['TestController@post']]) }}
Test1
{{ Form::hidden('test1', "test1")}}
{{ Form::submit('CONTINUE')}}
{{ Form::close() }}
test2.blade.php : test2.blade.php:
{{ Form::open(['action' => ['TestController@post']]) }}
test2
{{ Form::hidden('test2', "test2") }}
{{ Form::submit('CONTINUE')}}
{{ Form::close() }}
When return \\Redirect::back()->withErrors(["my error"]);
return \\Redirect::back()->withErrors(["my error"]);
executes i'm expecting to return to test2.blade.php but i'm returning to test.blade.php. 执行,我希望返回到test2.blade.php,但是我要返回到test.blade.php。 How can I fix this?
我怎样才能解决这个问题?
You shouldn't be using return view()
inside a post
request. 您不应该在
post
请求中使用return view()
。
This is an example of a request cycle from a shop: 这是来自商店的请求周期的示例:
GET - show view 1 (shopping-cart)
POST - it's a form submmited from view 1
- if it fails, it redirect to view 1
- after doing the app logic, it redirects to view 2
GET - show view 2 (payment-method)
POST - it's a form submmited from view 2
- if it fails, redirect back will go to view 2
- if it succeded, should redirect to view 3
GET - show view 3 (order confirmation)
Your problem will disappear with this route structure 您的问题将通过这种路线结构消失
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.