简体   繁体   English

Laravel 5.4:当我有3个帖子页面使用相同的网址时,如何重定向回正确的页面?

[英]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.

相关问题 当Laravel 5.4中的验证失败时,我该如何重定向回去? - How do I redirect back when validation fails in Laravel 5.4? 使用发布/重定向/获取,但是当页面被重定向回时,我仍然必须刷新 - Using Post/Redirect/Get but when page gets re-directed back I still have to refresh Laravel 5.4-当模型有两个外来对象时如何获得正确的关系 - Laravel 5.4 - How I can get the correct relationship when model have 2 foreigns Laravel 5.4重定向回预期的URL - Laravel 5.4 Redirect back to intended url 如何绕过URL中的公用文件夹(Laravel 5.4) - How do I by-pass the Public Folder in the url (Laravel 5.4) 使用Join且2个表的字段名称相同时,如何在MYSQL查询中查明正确的结果? - How do I pinpoint correct result in MYSQL Query when using Join and the 2 tables have field with same name? 如果未在Laravel 5.4中进行身份验证,我想重定向登录页面 - I want to redirect login page if not authenticate in Laravel 5.4 如果匿名用户无权访问 URL,如何将匿名用户重定向到自定义页面而不是登录页面? - How do I redirect an anonymous user to custom page instead of the login page if they do not have access to a URL? 如何使用 Laravel 5.4 注销并重定向到登录页面? - How to logout and redirect to login page using Laravel 5.4? 在Laravel 5.4应用中,当我尝试登录时,它会重定向到同一登录页面 - In a Laravel 5.4 app, when I try to login, it redirects to same login page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM