简体   繁体   English

提交创建表单后,Laravel重定向回到列表

[英]Laravel redirecting back to list after submitting create form

Maybe I'm missing something but I can't figure out how to do this. 也许我缺少了一些东西,但是我不知道该怎么做。

I have a index view that lists all the records from my tiers table. 我有一个索引视图,该视图列出了我的层表中的所有记录。 The page has a "Create New Tier" button that displays a form. 该页面具有显示表单的“创建新层”按钮。 The user enters the data for the new tier and hits the submit button. 用户输入新层的数据,然后单击“提交”按钮。 In my TierController I have the following: 在我的TierController中,我有以下内容:

public function store()
{
//
$data = Input::all();

$tier = new Tier();
$tier->name=$data['name'];
$tier->price = $data['price'];

$tier->save();

return Redirect::back()->with('message',"Tier created successfully.");
}

This takes me back to the empty create form and displays the message but the user has to press the back button to return to the index view which doesn't show the new data until I hit refresh. 这将我带回到空的创建表单并显示消息,但是用户必须按“后退”按钮返回到索引视图,该索引视图在我单击刷新后才显示新数据。 I would like to have the system automatically return to the index view after the submit button is clicked with the refreshed data. 我希望在单击带有刷新数据的“提交”按钮后,系统自动返回到索引视图。

I know that I can do a Redirect::to('tiers.index')->with('message','Tier created successfully') but the problem I have with that is then the browser history looks like 我知道我可以做一个Redirect :: to('tiers.index')-> with('message','Tier创建成功'),但是我遇到的问题是浏览器历史记录看起来像

tier.index -> tier.create -> tier.index

This then is very confusing for the user when they do hit the back button as the create view shows up and freaks them out. 当用户确实按下后退按钮时,这将使用户感到非常困惑,因为创建视图会出现并吓坏他们。

Is there a way to do a Redirect::back (or something) that would not leave the tier.create view in the history and also refresh the index data? 有没有一种方法可以做到Redirect :: back(或其他方式),而不会在历史记录中保留tier.create视图,也不会刷新索引数据?

Thanks 谢谢

I don't think you can manipulate the user's browsing history without resorting to Javascript or something. 我认为您不借助Javascript或其他方法就无法操纵用户的浏览历史记录。 Certainly not with Laravel alone. 当然不是仅凭Laravel。 So, don't . 所以,不要 You're overthinking this. 您想得太多了。 Why do you care so much about what's in the browser's history? 您为什么如此在意浏览器历史记录中的内容? And why would you want to manipulate it so that it doesn't reflect the pages the user actually went through? 以及为什么要操纵它以使其不反映用户实际浏览的页面? It sort of defeats the purpose of having a history... 这有点破坏了拥有历史的目的。

If your app's navigation is sufficiently intuitive and familiar, the user won't feel the need to use the native browser's back / forward buttons. 如果您的应用导航非常直观和熟悉,那么用户将不会感到需要使用本机浏览器的后退/前进按钮。 Don't assume your users will make the most basic browsing mistakes unless you have data to back that up. 除非您有数据来备份,否则不要以为用户会犯最基本的浏览错误。

Having said that, conventions don't give you much choice in this matter: generally you either lead the user back to the index -- ie Redirect::route('tiers.index') -- or to the newly created item -- ie Redirect::route('tiers.show', $tier->id) . 话虽如此,约定在这件事上并没有给您太多选择:通常,您要么将用户带回到索引(即Redirect::route('tiers.index') ,要么回到新创建的项-即Redirect::route('tiers.show', $tier->id) Both are perfectly acceptable and surely within the user's expected outcome. 两者都是完全可以接受的,并且肯定在用户的预期结果之内。

If tiers are something the user can only add within certain limits (one per user, one per category, once a day, etc), then I'd attempt to redirect him away from the create page, preventing him from filling out a fresh form which won't ever pass validation. 如果tiers是用户只能在特定限制内添加的tiers (每个用户一个,每个类别一个,每天一次,等等),那么我将尝试将他重定向到创建页面之外,以防止他填写新表单永远不会通过验证。 Otherwise, chose one of the pages above and move on to your next development challenge. 否则,请选择以上页面之一,然后继续进行下一个开发挑战。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM