Laravel-admin/Dcat admin自定义工具(清除站点缓存文件)

宰父劲
2023-12-01

laravel-admin

1,自定义工具
(1)打开app/Admin/bootstrap.php:追加一下代码

use Encore\Admin\Facades\Admin;
Admin::navbar(function (\Encore\Admin\Widgets\Navbar $navbar) {

    $navbar->right(new \App\Admin\Extensions\Clear());

});

(2)在app/admin目录新建Extensions目录
。。。。
(3)在Extensions目录下新建PHP class 文件Clear,code:

<?php
namespace App\Admin\Extensions;

use Think\Exception;
use Throwable;
use Encore\Admin\Admin;
use App\Imports\DataExcel;
use Encore\Admin\Actions\Action;
use Encore\Admin\Actions\Response;
use Illuminate\Support\Facades\Artisan;
class Clear extends Action
{

    protected $selector = '.import-template';
    public function handle()
    {

        try {
            Artisan::call('cache:clear');
        } catch (Exception $e) {

        }

        return $this->response()->success('清理完成')->refresh();
    }

    // 按钮样式
    public function html()
    {
        return <<<HTML
       <li>
            <a href="#" class="btn  import-template">
              <i class="fa fa-trash"></i>
              清理缓存
            </a>
        </li>
HTML;
    }
}

Dcat admin

Dcat Admin是一个基于laravel-admin二次开发而成的后台系统构建工具,只需极少的代码即可快速构建出一个功能完善的高颜值后台系统。支持页面一键生成CURD代码,内置丰富的后台常用组件,开箱即用,让开发者告别冗杂的HTML代码,对后端开发者非常友好。
1.在app/admin 下bootstrap.php 中增加

Admin::navbar(function (Navbar $navbar) {

    $navbar->right(\App\Admin\Actions\Clear::make()->render());

});

2.需要在admin下新建Actions文件夹,新建文件 Clear.php 加入以下代码

<?php

namespace App\Admin\Actions;

use Dcat\Admin\Actions\Action;
use Dcat\Admin\Actions\Response;
use Dcat\Admin\Traits\HasPermissions;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use Dcat\Admin\Widgets\Modal;
class Clear extends Action
{
    protected $title = '清理缓存';

    public function render()
    {
        $modal = Modal::make()
            ->id('clear') // 导航栏显示弹窗,必须固定ID,随机ID会在刷新后失败
            ->title($this->title())
            ->body(\App\Admin\Actions\Form\Clear::make())
            ->lg()
            ->button(
                <<<HTML
<ul class="nav navbar-nav" style="margin-right: 2rem">
    <li><i class="ficon feather icon-trash-2"></i>{$this->title}</li>
</ul>
HTML
            );

        return $modal->render();
    }
}

3.在Actions下新建Form目录,新建Clear.php

<?php

namespace App\Admin\Actions\Form;

use Dcat\Admin\Actions\Response;
use Dcat\Admin\Form\AbstractTool;
use Dcat\Admin\Traits\HasPermissions;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Artisan;
use PHPUnit\Runner\Exception;

class Clear extends AbstractTool
{
    /**
     * @return string
     */
	protected $title = '点击继续';

    /**
     * Handle the action request.
     *
     * @param Request $request
     *
     * @return Response
     */
    public function handle(Request $request)
    {

        try {
            Artisan::call('cache:clear');
            return $this->response()
                ->success('清理成功')
                ->redirect('/');
        } catch (Exception $e) {
            return $this->response()
                ->success('清理失败')
                ->redirect('/');
        }
    }

    /**
     * @return string|void
     */
    protected function href()
    {
        // return admin_url('auth/users');
    }

    /**
	 * @return string|array|void
	 */
	public function confirm()
	{
		 return ['Confirm?', 'contents'];
	}

    /**
     * @param Model|Authenticatable|HasPermissions|null $user
     *
     * @return bool
     */
    protected function authorize($user): bool
    {
        return true;
    }

    /**
     * @return array
     */
    protected function parameters()
    {
        return [];
    }
}

 类似资料: