bulforce/ext-direct

此包的最新版本(dev-master)没有可用的许可证信息。

Laravel ExtJS/Sencha direct 提供者

dev-master 2017-04-28 14:14 UTC

This package is not auto-updated.

Last update: 2024-09-22 07:54:06 UTC


README

##信息 这是为 Laravel 5 和 ExtJS 4(也兼容 ExtJS 5)提供的 ext-direct 提供者

寻找 Laravel 4 包,请检查 laravel4 分支。

ext-direct 的理念是允许 JavaScript 以客户端 JavaScript 方法的方式调用远程 PHP 方法。这大大减少了开发时间。

几乎全部(99%)基于 http://www.sencha.com/forum/showthread.php?102357-Extremely-Easy-Ext.Direct-integration-with-PHP

原始类只稍作修改。

##安装##

将此行添加到 composer.json 的 require 列表,并运行 composer update

"bulforce/ext-direct": "dev-master"

将此添加到 app.php 的 'providers' 数组中

'Bulforce\ExtDirect\ExtDirectServiceProvider', 

此包包含一个 Facade,但您无需将其包含在 app.php 中,它将自动包含。

必须发布或创建此配置文件

../laravel_project/app/config/packages/bulforce/ext-direct/config.php

必须编辑 config.php 文件

<?php

return array(
    'namespace' => 'Ext.rpc',
    'descriptor' => 'Ext.rpc.REMOTING_API',
    'timeout' => 30,
    'debug' => true,
    'api_classes' => array(
        'Items' => 'ItemsController'
    )
);

最重要的部分是 api_classes 数组,在那里您必须列出所有您希望 extjs 直接调用的应用程序中的类(通常是控制器)。

注意:它不必须是关联数组,您可以简单地在一个普通索引数组中列出类名。但是,如果您以关联数组的形式列出它们,则可以使用数组元素 key 而不是实际的控制器类名来调用它们。这样,您可以隐藏您真实的应用程序结构,使其不会在前端暴露。

标签直接方法 为了使控制器方法可用于 extjs/sencha 直接调用,必须满足以下两个条件

1. Method needs to be declared as **public**
2. Method needs to contain comment tag @direct

示例

    /**
     * @direct
     */
    public function read($params = null) {
        return Item::take(50)->get();
    }

在 routes.php 中添加路由

    Route::any('/rpc', function() {
        return ExtDirect::provide();
    });

最后,在 index.html 中添加类似以下内容(在 extjs 库之后和您的应用程序代码之前!!!)

<script type="text/javascript" src="http://laravel_project.dev/rpc?javascript"></script>

现在,您应该能够直接从 JavaScript 调用 Laravel 控制器方法

Ext.rpc.Items.read(function(response) {
    console.log(response);
});

//with params, params will be passed to the controller method as php object
Ext.rpc.Items.read({page: 5},function(response) {
    console.log(response);
});

//use it in direct stores api object
api: {
    read: Ext.rpc.Items.read
}