jaxon-php/jaxon-zend

此包已被废弃,不再维护。未建议替代包。

Zend Framework 2 的 Jaxon 库集成

v3.1.0 2020-01-13 01:27 UTC

README

此包将 Jaxon 库与 Zend Framework 2.3+ 和 3 集成。

功能

  • 自动注册预设目录中的 Jaxon 类。
  • 从配置文件中读取 Jaxon 选项。

安装

composer.json 文件中添加以下行,并运行 composer update 命令。

"require": {
    "jaxon-php/jaxon-zend": "~3.1"
}

将 Jaxon 模块添加到 config/application.config.phpconfig/modules.config.php 配置文件中的 modules 条目。

    'modules' => [
        'Application',
        'Jaxon\Zend',
    ),

Zend Framework 2

按如下方式编辑 module/Application/config/module.config.php 文件。

  1. 导入当前命名空间中的 Jaxon 类
use Jaxon\Zend\Factory\Zf2ControllerFactory;
  1. 使用 Service Manager 注册 Jaxon 插件
    'service_manager' => [
        'invokables' => [
            'JaxonPlugin' => 'Jaxon\Zend\Controller\Plugin\JaxonPlugin',
        ),
    ),
  1. 使用提供的工厂创建应用程序控制器和 Jaxon ZF 控制器。
    'controllers' => [
        'factories' => [
            'Application\Controller\Demo' => Zf2ControllerFactory::class,
            'Jaxon\Zend\Controller\Jaxon' => Zf2ControllerFactory::class,
        ),
    ),

此工厂将 Jaxon 插件注入到 ZF 控制器构造函数中。

  1. 将 Jaxon 请求 URI 路由到插件控制器。
    'router' => [
        'routes' => [
            // Route to the Jaxon request processor
            'jaxon' => [
                'type' => 'Zend\Mvc\Router\Http\Literal',
                'options' => [
                    'route'    => '/jaxon',
                    'defaults' => [
                        'controller' => 'Jaxon\Zend\Controller\Jaxon',
                        'action'     => 'index',
                    ),
                ),
            ),
        ),
    ),

Zend Framework 3

按如下方式编辑 module/Application/config/module.config.php 文件。

  1. 导入当前命名空间中的 Jaxon 类
use Jaxon\Zend\Factory\Zf3ControllerFactory;
use Jaxon\Zend\Controller\Plugin\JaxonPlugin;
use Jaxon\Zend\Controller\JaxonController;
  1. 使用 Service Manager 注册 Jaxon 插件
    'service_manager' => [
        'invokables' => [
            'JaxonPlugin' => JaxonPlugin::class,
        ],
    ],

或者

    'service_manager' => [
        'factories' => [
            JaxonPlugin::class => InvokableFactory::class,
        ],
        'aliases' => [
            'JaxonPlugin' => JaxonPlugin::class,
        ],
    ],
  1. 使用提供的工厂创建应用程序控制器和 Jaxon ZF 控制器。
    'controllers' => [
        'factories' => [
            Controller\DemoController::class => Zf3ControllerFactory::class,
            JaxonController::class => Zf3ControllerFactory::class,
        ],
    ],

此工厂将 Jaxon 插件注入到 ZF 控制器构造函数中。

  1. 将 Jaxon 请求 URI 路由到 Jaxon 控制器
    'router' => [
        'routes' => [
            // Route to the Jaxon request processor
            'jaxon' => [
                'type' => Literal::class,
                'options' => [
                    'route'    => '/jaxon',
                    'defaults' => [
                        'controller' => JaxonController::class,
                        'action'     => 'index',
                    ],
                ],
            ],
        ],
    ],

配置

Jaxon 库的配置定义在 config/jaxon.config.php 文件中。一个示例配置文件在 此处 提供。

配置文件中的 config/jaxon.config.php 设置分为两个部分。位于 lib 部分的选项是 Jaxon 核心库的选项,而位于 app 部分的选项是 Zend Framework 应用程序的选项。

以下选项可以在配置文件的 app 部分中定义。

名称 描述
directories 包含 Jaxon 应用程序类的目录数组
views 包含 Jaxon 应用程序视图的目录数组

默认情况下,views 数组为空。视图从框架默认位置渲染。在 directories 数组中有一个条目,其值如下。

名称 默认值 描述
directory {app_dir}/jaxon/Classes Jaxon 类的目录
namespace \Jaxon\App Jaxon 类的命名空间
separator . Jaxon 类名中的分隔符
protected 空数组 防止 Jaxon 导出某些方法

用法

这是一个使用 Jaxon 库的 Zend Framework 控制器的示例。

namespace Application\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Jaxon\Zend\Controller\Plugin\JaxonPlugin;

class DemoController extends AbstractActionController
{
    /**
     * @var \Jaxon\Zend\Controller\Plugin\JaxonPlugin
     */
    protected $jaxon;

    public function __construct(JaxonPlugin $jaxon)
    {
        $this->jaxon = $jaxon;
    }

    public function indexAction()
    {
        $view = new ViewModel([
            'jaxonCss' => $this->jaxon->css(),
            'jaxonJs' => $this->jaxon->js(),
            'jaxonScript' => $this->jaxon->script(),
        ]);
        $view->setTemplate('demo/index');
        return $view;
    }
}

在打印页面之前,控制器调用 $jaxon->css()$jaxon->js()$jaxon->script() 函数来获取 Jaxon 生成的 CSS 和 JavaScript 代码,并将其插入到页面中。

Jaxon 类

Jaxon 类可以继承自 \Jaxon\App\CallableClass。默认情况下,它们从 Zend Framework 应用程序根目录下的 jaxon/Classes 目录加载,关联的命名空间是 \Jaxon\App

这是一个简单的 Jaxon 类示例,定义在 jaxon/Classes/HelloWorld.php 文件中。

namespace Jaxon\App;

class HelloWorld extends \Jaxon\App\CallableClass
{
    public function sayHello()
    {
        $this->response->assign('div2', 'innerHTML', 'Hello World!');
        return $this->response;
    }
}

请求处理

默认情况下,Jaxon 请求由 src/Controller/JaxonController.php 文件中的控制器处理。默认情况下,/jaxon 路由链接到 JaxonController::actionIndex() 方法。

贡献

  • 问题追踪:github.com/jaxon-php/jaxon-zend/issues
  • 源代码:github.com/jaxon-php/jaxon-zend

许可证

该软件包采用 BSD 许可证。