Laravel:发布后加载视图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21600349/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel : Load a view after posting
提问by The Dude
In Codeigniter I used to call the view function after posting data. Like below;
在 Codeigniter 中,我曾经在发布数据后调用视图函数。如下图;
Ex: I have a show_products() function which will display list of products. When a user add a new product I'm posting data into add_product() function. If the process is successful I will notredirect to the products page, instead load the display function inside the add_product() like this:
例如:我有一个 show_products() 函数,它将显示产品列表。当用户添加新产品时,我将数据发布到 add_product() 函数中。如果该过程成功,我将不会重定向到产品页面,而是像这样在 add_product() 中加载显示函数:
//Inside the add_product() function
if(success){
$this->show_products();
}
I think, there is no point of reloading the page again. Since we are already in the post function we can straight away set the view after the database insert.
我认为,没有必要再次重新加载页面。由于我们已经在 post 函数中,我们可以在数据库插入后立即设置视图。
However in laravel I see people redirecting after posting data.
但是在 laravel 中,我看到人们在发布数据后重定向。
ex:
前任:
//Inside the postProduct() function
if(success){
return Redirect::to('products');
}
I tried;
我试过;
//Inside the postProduct() function
if(success){
$this->getIndex();// this is my product display function
}
but it didn't work.
但它没有用。
Do we have a benefit by loading the view in the post function without redirecting every time?
通过在 post 函数中加载视图而无需每次都重定向,我们是否有好处?
If so how can I achieve the same thing with the laravel?
如果是这样,我怎样才能用 laravel 实现同样的目标?
Thanks a lot!
非常感谢!
回答by The Alpha
It's not about the Laravel
, instead, it's about a good or right way of doing things. In other words, it's a good programming practice to redirect to another route/page after you successfully submit a form.
这不是关于Laravel
,而是关于一种好的或正确的做事方式。换句话说,在成功提交表单后重定向到另一个路由/页面是一个很好的编程实践。
Even in CodeIgniter
or plain Php
I do like this approach and encourage other developers to do that. So, the question is why this redirect is better than directly calling another method from the same request to show another view/page ?
即使是CodeIgniter
平淡无奇,Php
我也喜欢这种方法并鼓励其他开发人员这样做。所以,问题是为什么这种重定向比直接从同一个请求调用另一个方法来显示另一个视图/页面更好?
This is the life cycle of the process:
这是进程的生命周期:
- You post a form to a route/action page.
- You validate the submitted data and upon successful validation you insert the submitted data in to your database, otherwise you redirect back to that form with errors and old user inputs.
- 您将表单发布到路由/操作页面。
- 您验证提交的数据,并在成功验证后将提交的数据插入到您的数据库中,否则您重定向回该表单,并显示错误和旧的用户输入。
So. assume that, you have submitted a form and done saving the data into database successfully. After you save it you done something like this:
所以。假设您已提交表单并成功将数据保存到数据库中。保存后你做了这样的事情:
return View::make('...')->with('success', 'Data saved!');
In this case, your user can see the success
message on the screen but what if the user, presses the f5key from the keyboard to refresh
the page (probably, accidentally), the form will be submitted to the same action again and the whole process will be repeated again.
在这种情况下,您的用户可以success
在屏幕上看到消息,但是如果用户按下f5键盘上refresh
的键到页面(可能是不小心),表单将再次提交到相同的操作,整个过程将是又重复了一遍。
So, if you had a redirect after form submission then, refreshing
the page won't make any request to that form again.
因此,如果您在提交表单后进行了重定向,则refreshing
该页面将不会再次向该表单发出任何请求。
Google search result on form resubmit on refresh., check the links, may be first one/two, you'll get better idea about the problem and the benefits of redirection after form submission.
刷新时重新提交表单上的Google 搜索结果。,检查链接,可能是第一个/两个,您将更好地了解问题以及提交表单后重定向的好处。
回答by Moorthy GK
in Codeigniter to redirect page we have redirect()
function.
在 Codeigniter 中重定向页面我们有redirect()
功能。
if(success){
redirect('products');
}
回答by Tom
You don't have to return Redirect
. The reason people use it quite often in larvel is because it's comfy.
你不必返回Redirect
。人们在幼虫中经常使用它的原因是因为它很舒服。
You can return something else, eg. a view:
您可以返回其他东西,例如。一个看法:
return View::make('home.index')->with('var',$var);
回答by Andrew
In Laravel, to redirect after doing a POST, you could return a redirect with a named route:
在 Laravel 中,要在执行 POST 后重定向,您可以返回带有命名路由的重定向:
return redirect()->route('my-route-name');
Or if you are within the controller that has the route method you want (eg. the index
method, you could do this as well:
或者,如果您在具有所需路由方法的控制器中(例如,该index
方法,您也可以这样做:
return self::index();