如何自定义laravel背包中的取消按钮?

How to custom cancel button in laravel backpack?

首先,这是我的网站看起来像

Web Interface

我有这个取消按钮,我想自定义它,所以当我点击它时,它会重定向到另一个页面。 请回答这个谢谢

此功能处于 4.2 版的开发阶段issue lnk

但您也可以自己添加此功能。从 vendor 文件夹发布你的背包库。在 resources/views/vendor/backpack/crud/buttons 中创建一个名为 cancel.blade.php 的文件并粘贴以下代码。

@php
/*
    To override all cancel button urls with your custom url you should create a crud macro in your service provider
    We don't need to pass any parameter because there `$this` will be the crud panel.
*/
if($crud->hasMacro('cancelButtonUrl') && is_null($crud->getOperationSetting('cancelButtonUrl'))) {
    $cancel_button_href = $crud->cancelButtonUrl();
}else{
    //if developer provided a closure, run the closure.
    if(is_callable($crud->getOperationSetting('cancelButtonUrl'))) {
        $cancel_button_href = $crud->getOperationSetting('cancelButtonUrl')($crud);
    }else{
        //if nothing is specified about cancel button url we use the backpack defaults
        if(is_null($crud->getOperationSetting('cancelButtonUrl'))) {
            $cancel_button_href = $crud->hasAccess('list') ? url($crud->route) : url()->previous();
        }else{
            //developer might use a url string here if he does not need any customization in the closure.
            $cancel_button_href = $crud->getOperationSetting('cancelButtonUrl');
        }
    }
}
@endphp
@if(!$crud->hasOperationSetting('showCancelButton') || $crud->getOperationSetting('showCancelButton') == true)
<a href="{{ $cancel_button_href }}" class="btn btn-default"><span class="la la-ban"></span> &nbsp;{{ trans('backpack::crud.cancel') }}</a> 
@endif

在 resources/views/vendor/backpack/crud/inc 目录中创建另一个名为 form_save_buttons.blade.php 的文件并粘贴以下代码。

@if(isset($saveAction['active']) && !is_null($saveAction['active']['value']))
<div id="saveActions" class="form-group">

    <input type="hidden" name="save_action" value="{{ $saveAction['active']['value'] }}">
    @if(!empty($saveAction['options']))
        <div class="btn-group" role="group">
            @endif

            <button type="submit" class="btn btn-success">
                <span class="la la-save" role="presentation" aria-hidden="true"></span> &nbsp;
                <span data-value="{{ $saveAction['active']['value'] }}">{{ $saveAction['active']['label'] }}</span>
            </button>

            <div class="btn-group" role="group">
                @if(!empty($saveAction['options']))
                    <button id="btnGroupDrop1" type="button" class="btn btn-success dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"><span class="caret"></span><span class="sr-only">&#x25BC;</span></button>
                    <div class="dropdown-menu" aria-labelledby="btnGroupDrop1">
                        @foreach( $saveAction['options'] as $value => $label)
                            <a class="dropdown-item" href="javascript:void(0);" data-value="{{ $value }}">{{ $label }}</a>
                        @endforeach
                    </div>

                @endif
            </div>
            @if(!empty($saveAction['options']))
        </div>
    @endif

    @include('crud::buttons.cancel')
</div>
@endif

现在在您的 crud 控制器中使用此代码将 URL 添加到取消按钮。

$this->crud->setOperationSetting(
        'cancelButtonUrl',
        function($crud) {
            return 'https://omg.com';
        },
        'create' //this can be ommited if you are adding in a specific operation
    );