jaxon-php/jaxon-yii

Yii 框架的 Jaxon 库集成

v4.0.0 2022-07-30 14:30 UTC

README

此包将 Jaxon 库集成到 Yii 框架中。它需要 2.0.11 或更高版本。

安装

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

"require": {
    "jaxon-php/jaxon-yii": "^4.0"
}

或者运行 composer require jaxon-php/jaxon-yii 命令。

此包提供了一个过滤器,必须将其附加到启用了 Jaxon 功能的页面路由。

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

namespace app\controllers;

use Yii;
use yii\web\Controller;
use Jaxon\Yii\Filter\JaxonConfigFilter;

class DemoController extends Controller
{
    public function behaviors()
    {
        return [
            [
                'class' => JaxonConfigFilter::class,
                'only' => ['index', 'jaxon'],
            ],
        ];
    }

    /**
     * Process Jaxon ajax requests. This route must be the same that is set in the Jaxon config.
     */
    public function actionJaxon()
    {
        $jaxon = jaxon()->app();
        if(!$jaxon->canProcessRequest())
        {
            // Jaxon failed to find a plugin to process the request
            return; // Todo: return an error message
        }

        $jaxon->processRequest();
        return $jaxon->httpResponse();
    }

    /**
     * Insert Jaxon js and css codes in the page.
     */
    public function actionIndex()
    {
        // Set the layout
        $this->layout = 'demo';
        // Get the Jaxon module
        $jaxon = jaxon()->app();

        return $this->render('index', [
            'jaxonCss' => $jaxon->css(),
            'jaxonJs' => $jaxon->js(),
            'jaxonScript' => $jaxon->script()
        ]);
    }
}

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

配置

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

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

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

用法

Jaxon 类

Jaxon 类可以继承自 \Jaxon\App\CallableClass。默认情况下,它们位于 Yii 应用程序的 @app/jaxon/ajax 目录中,相关命名空间为 \Jaxon\Ajax

这是一个 Jaxon 类的示例,定义在 @app/jaxon/ajax/HelloWorld.php 文件中。

namespace Jaxon\Ajax;

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

贡献

  • 问题跟踪器:github.com/jaxon-php/jaxon-yii/issues
  • 源代码:github.com/jaxon-php/jaxon-yii

许可

此包在 BSD 许可下发布。