modera/direct-bundle

Symfony ExtDirect 扩展包

安装次数: 5,781

依赖项: 1

建议者: 0

安全: 0

星标: 2

关注者: 6

分支: 1

公开问题: 0

类型:symfony-bundle

v3.1.0 2021-10-13 06:33 UTC

README

ModeraDirectBundle 是 ExtDirect 规范在 Symfony 框架中的实现。

安装

步骤 1: 下载扩展包

composer require modera/direct-bundle:4.x-dev

此命令要求您全局安装 Composer,如 Composer 文档中的安装章节所述。

步骤 2: 启用扩展包

此扩展包应通过 Flex 自动启用。如果您不使用 Flex,您需要手动在项目的 config/bundles.php 文件中添加以下行以启用扩展包

<?php
// config/bundles.php

return [
    // ...
    Modera\DirectBundle\ModeraDirectBundle::class => ['all' => true],
];

步骤 3: 添加路由

// config/routes.yaml

direct:
    resource: "@ModeraDirectBundle/Resources/config/routing.yml"

如何使用

将 ExtDirect API 添加到您的页面

如果您使用 Twig 引擎,只需在视图页面的脚本部分添加以下行

<script type="text/javascript" src="{{ url('api') }}"></script>

或者如果您不使用模板引擎

<script type="text/javascript" src="/api.js"></script>

将控制器方法暴露给 ExtDirect Api

// .../Acme/DemoBundle/Controller/ExampleController.php

namespace Acme\DemoBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Modera\DirectBundle\Annotation\Remote;
use Modera\DirectBundle\Annotation\Form;

class ExampleController extends AbstractController
{
   /**
    * Single exposed method.
    *
    * @Remote    // this annotation expose the method to API
    *
    * @param  array $params
    * @return string
    */
    public function indexAction(array $params)
    {
        return 'Hello ' . $params['name'];
    }

    /**
     * An action to handle forms.
     *
     * @Remote   // this annotation expose the method to API
     * @Form     // this annotation expose the method to API with formHandler option
     *
     * @param array $params Form submitted values
     * @param array $files  Uploaded files like $_FILES
     */
    public function testFormAction(array $params, array $files)
    {
        // your proccessing
        return true;
    }
}

从 JavaScript 调用暴露的方法

// 'AcmeDemo' is the Bundle name without 'Bundle'
// 'Example' is the Controller name without 'Controller'
// 'index' is the method name without 'Action'
Actions.AcmeDemo_Example.index({ name: 'ExtDirect' }, function(r) {
   alert(r);
});

许可

此扩展包采用 MIT 许可。请参阅扩展包中的完整许可:Resources/meta/LICENSE