Laravel One Form Two提交按钮

时间:2015-04-25 12:34:11

标签: php laravel laravel-5

我有表单视图;

<form class="form-horizontal" role="form" method="GET" action="">

@foreach($input as $key => $value)
    @foreach($value as $subkey => $subvalue)                            
        {!! Form::hidden($key.'_'.$subkey, $subvalue) !!}
    @endforeach
@endforeach

{!! Form::hidden('postcode_id', $postcode_lookup['id']) !!}

<input class="btn btn-info"    type="submit" name="action" value="Map View"     />
<input class="btn btn-warning" type="submit" name="action" value="CSV Download" />

</form>

我有Routes.php代码;

if ( Input::get('action') === 'CSV Download' )
{
  Route::get('/export/csv', 'ExcelController@index');
}

if ( Input::get('action') === 'Map View' )
{
  Route::get('/map/all', 'MapController@index');
}

如果用户点击一个按钮,我希望他们调用ExcelController。如果它是另一个,我想要MapController调用...两个不同的控制器!

我的action路线应该是什么?目前,当我点击它时,它只会停留在当前页面上。

2 个答案:

答案 0 :(得分:1)

操作路线为空,因此指向当前页面。它应该是某种途径。例如

<.. action="{{ URL::to('split_form') }}" ..>

然后,您可以将过滤器附加到您的路线(在routes.php中):

Route::get('split_form', array('before' => 'form_splitter'));

并定义此过滤器(在filters.php中)

Route::filter('form_splitter', function()
{
    if ( Input::get('action') === 'CSV Download' )
    {
        Redirect::to('/export/csv');
    }
    elseif ( Input::get('action') === 'Map View' )
    {
       Redirect::to('/map/all');
    }
});

并定义实际路线(在routes.php中)

Route::get('/export/csv', 'ExcelController@index');
Route::get('/map/all', 'MapController@index');

虽然angelo在blackbischop的第二个链接中的答案也可以满足您的需求,但使用javascript保存过滤器/重定向

答案 1 :(得分:0)

您可以对提交按钮使用相同的名称和不同的值属性

//示例:

<input type="submit" class="btn btn-success" value="save and close" name="submitbutton">
<input type="submit" class="btn btn-success" value="apply" name="submitbutton">
            

//控制器:

switch($request->submitbutton) {

    case 'save and close': 
        //action save here and close
    break;

    case 'apply': 
        //action for save and route here
    break;
}

    if ($request->submitbutton == 'apply') {
        return redirect()->route('admin.products.edit', $product->id)->with('success', "new product {$product->name} created as well.");
    } else if ($request->submitbutton == 'save and close'){
        return redirect()->route('admin.products.index')->with('info', "product {$product->name} saved");
    } 

我的完整答案在这里https://stackoverflow.com/a/63087033/308578