Joseph-Montanez/laravel-atk

Laravel的敏捷UI和数据集成

v1.0.4 2018-10-13 05:19 UTC

This package is auto-updated.

Last update: 2024-09-04 11:45:15 UTC


README

Laravel ATK是Agile UI和Agile Data的Laravel集成包。

安装

首先通过Composer安装此包。请在终端运行以下命令

composer require joseph-montanez/laravel-atk

添加中间件

Agile UI可以在调用$app->run()之前终止。目前这意味着会抛出异常,所以为了捕获异常并返回响应,需要Laravel的中间件。

编辑app/Http/Kernel.php

<?php

namespace App\Http;

use Illuminate\Foundation\Http\Kernel as HttpKernel;

class Kernel extends HttpKernel
{
    //....

    /**
     * The application's route middleware groups.
     *
     * @var array
     */
    protected $middlewareGroups = [
        'web' => [
            //....
            // Add this middleware line to web
            \Atk\Laravel\Middleware\AgileUiTerminated::class,
        ],

##用法

Agile UI集成

这个库是为Laravel设计的应用层。您需要在控制器中使用这个应用层才能利用Agile UI。如果不这样做,您会得到500错误,cookies和/或会话可能无法保存,并且URL将添加.php

1. 初始化Agile UI

通常要初始化Agile UI,您会调用以下代码

$app = new \atk4\ui\App();

但由于存在自定义应用层,您有两个选择

//-- Directly call the integrated app class
$app = new \Atk\Laravel\Ui\App(['title' => 'My Admin']);

//-- Using Laravel's service provider to keep track of app instance
$app = app()->make(\Atk\Laravel\Ui\App::class, ['title' => 'My Admin']);

2. 在控制器中使用Agile UI

要使Agile UI渲染,您需要返回一个响应,因为输出已被禁用。

class RoleController
{
    public function index(Request $request, DB $db, Common $ui)
    {
        $app = app()->make(\Atk\Laravel\Ui\App::class, ['title' => 'My Admin']);

        /**
         * The List Header
         *
         * @var \atk4\ui\Header $header
         */
        $header = $ui->app->add('Header', ['Hello There!']);

        return response($app->run());
    }
}

3. 在链接中使用Laravel的路由名称生成器

如果您依赖于Laravel的路由名称生成器,那么您需要使用此包的变体Link。这允许将记录/行数据传递给您的路由的回调。

/**
 * The User Grid
 *
 * @var \atk4\ui\Grid $grid
 */
$grid = $userListColumn->add('Grid');
$grid->setModel(new User($atkDb), ['name', 'email', 'phone', 'created_at']);
$grid->addDecorator(
    'name',
    new \Atk\Laravel\Ui\TableColumn\Link(null, [], function ($row) {
        return route('admin.user.edit', ['id' => $row['id']]);
    })
);

Agile数据库连接

如果您计划使用Agile Data,则可以使用服务提供者获取现有的数据库连接并将其传递给Agile Data。

$atkDb = app()->make(\Atk\Laravel\Data\Persistence_SQL::class);

/**
 * @var \atk4\ui\CRUD $crud
 */
$crud = $app->add('CRUD');
$crud->setModel(new Permission($atkDb),  ['name', 'group_name', 'guard_name', 'created_at']);