Laravel 4-同一页上有多个表格? [英] Laravel 4 - Multiple forms on same page?

查看:77
本文介绍了Laravel 4-同一页上有多个表格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

这就是它的样子,简化了:

Here's how it is, simplified:

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

Settings page: (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>

因此,基本上,同一页面上的两个单独的表单将发布到同一Controller中的两个单独的方法-方法成功后,它将它们重定向回设置".我不会发布方法,因为已经测试了它们并且它们可以工作,所以我相信问题出在路由文件中:

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

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');

    });
});

现在,我非常确定它不喜欢两条POST路由那样,但是由于表格位于同一页上,所以我无法想到另一种方式.我收到错误消息:

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:

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.

推荐答案

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

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 :

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

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

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

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

这篇关于Laravel 4-同一页上有多个表格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆