自己写的一个MethodLink工具。比如生成一个任意METHOD的link.

自己写的一个MethodLink工具。比如生成一个任意METHOD的link.

<?php
namespace App\Admin\Extensions;

use Encore\Admin\Admin;
use Encore\Admin\Widgets\Widget;
use Illuminate\Contracts\Support\Renderable;

class MethodLink extends Widget implements Renderable
{
    protected $link = '';

    protected $text = '';

    protected $method = 'GET';

    public function __construct($link = '', $method = 'GET', $text = '')
    {
        $this->link = $link;
        $this->method = $method;
        $this->text = $text;
    }

    public function attribute($attribute, $value = null)
    {
        if (is_array($attribute)) {
            $this->attributes = array_merge($this->attributes, $attribute);
        } else {
            $this->attributes[$attribute] = (string) $value;
        }
        return $this;
    }

    protected function getToken()
    {
        return csrf_token();
    }

    protected function script()
    {
        return <<<SCRIPT
$('a[data-method-link-select]').on('click', function () {var url = $(this).data('href');$.pjax({url: url,method:'POST',data: {_token:'{$this->getToken()}',_method:'{$this->method}',},container: '#pjax-container'});});
SCRIPT;
    }

    public function render()
    {
        Admin::script($this->script());
        return '<a data-method-link-select data-href="' . e($this->link) . '" ' . $this->formatAttributes() . '>' . $this->text . '</a>';
    }

}

列:

new MethodLink(admin_base_path('index'), 'POST', '我是POSTLINK')