Laravel 4 - 同一页面上有多个表单?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17338237/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 08:02:07  来源:igfitidea点击:

Laravel 4 - Multiple forms on same page?

phphtmllaravelblade

提问by Alias

I'm having some strange behavior with my forms in Laravel 4. I have a "settings" page with two forms, each (are supposed to) POST to a controller method, update the database and return back to the settings page. However, there seems to be an issue, either with the way my forms are working or my routes.

我在 Laravel 4 中的表单有一些奇怪的行为。我有一个包含两个表单的“设置”页面,每个(应该)POST 到一个控制器方法,更新数据库并返回到设置页面。但是,似乎存在问题,无论是我的表单的工作方式还是我的路线。

Here's how it is, simplified:

这是它的简化方式:

Settings page: (site.com/settings)

设置页面:(site.com/settings)

<div id="form-one" class="form-area">

{{ Form::open(array('action' => 'SettingController@editOption')) }}
   {{ Form::text('optionvalue', 'Default')) }}
   {{ Form::submit('Save Changes') }}
{{ Form::close() }}

</div>

<div id="form-two" class="form-area">

{{ Form::open(array('action' => 'SettingController@editPage')) }}
   {{ Form::text('pagevalue', 'Default')) }}
   {{ Form::submit('Save Changes') }}
{{ Form::close() }}

</div>

So basically, two seperate forms on the same page that post to two seperate methods in the same Controller - when the method is successful, it redirects them back to "settings". I won't post the methods, since tested them and they work, I believe the problem is in the routes file:

所以基本上,同一页面上的两个单独的表单发布到同一控制器中的两个单独的方法 - 当方法成功时,它会将它们重定向回“设置”。我不会发布这些方法,因为测试了它们并且它们有效,我相信问题出在路由文件中:

routes.php

路由文件

// Checks if a session is active
Route::group(array('before' => 'require_login'), function()
{   
    Route::group(array('prefix' => 'settings'), function()
    {
        Route::get('/', 'SettingController@index');
        Route::post('/', 'SettingController@editOption');
        Route::post('/', 'SettingController@editPage');

    });
});

Now I'm pretty sure it doesn't like the two POST routes being like that, however I cannot think of another way to do it, since the forms are on the same page. I get the error:

现在我很确定它不喜欢这样的两个 POST 路由,但是我想不出另一种方法来做到这一点,因为表单在同一页面上。我收到错误:

Unknown action [SettingController@editOption].

Since the option form comes first I guess. If I take the open form blade code out (for both), it loads the page - but obviously the form doesn't do anything.

由于选项表是第一位的,我猜。如果我取出开放表单刀片代码(对于两者),它会加载页面 - 但显然表单不会做任何事情。

Any help would be nice! Thanks in advance.

你能帮忙的话,我会很高兴!提前致谢。

采纳答案by oroshnivskyy

You can't add two same routes for different actions, because of they will be passed to first matched route and in your case to SettingController@editOption. Change your routes to :

您不能为不同的操作添加两个相同的路由,因为它们将被传递到第一个匹配的路由,在您的情况下会传递到SettingController@editOption. 将您的路线更改为:

    Route::post('/option', 'SettingController@editOption');
    Route::post('/page', 'SettingController@editPage');

Than in both actions you can redirect to '/': return Redirect::back(), and if error was occured:

在这两个操作中,您都可以重定向到 '/': return Redirect::back(),如果发生错误:

if ($validation->fails())
{
    return Redirect::to('/settings')->with_errors($validation);
}

回答by brunohdaniel

My alternative solution for this is to create an hidden html input in each form and make the controller identify what for is submitted based in this field. So, yu can use just one route for both.

我对此的替代解决方案是在每个表单中创建一个隐藏的 html 输入,并使控制器识别基于此字段提交的内容。所以,你可以只使用一条路线。