繁体   English   中英

Laravel 一表二提交按钮

[英]Laravel One Form Two Submit Buttons

我有我的表单视图;

<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路线应该是什么? 目前,当我单击其中一个时,它只停留在当前页面上。

动作路由为空,所以直接跳转到当前页面。 应该是某条路线。 例如

<.. 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为您节省了过滤器/重定向

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

// 例子:

<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

暂无
暂无

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

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