jaxon-php/jaxon-attributes 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

jaxon-php/jaxon-attributes

最新稳定版本:v1.2.0

Composer 安装命令:

composer require jaxon-php/jaxon-attributes

包简介

Attributes support for the Jaxon ajax PHP library

README 文档

README

Build Status Scrutinizer Code Quality codecov

Latest Stable Version Total Downloads License

Attributes for the Jaxon library

This package provides attribute support for the Jaxon library. The configuration options that are related to Jaxon classes can be set directly in the class files using attributes.

Installation

Install this package with composer. It requires jaxon-php/jaxon-core v5.1 or higher.

composer require jaxon-php/jaxon-attributes

Set the attribute config option.

jaxon()->setOption('core.metadata.format', 'attributes');

Note: The option must be set for a package if it defines classes with attributes.

When deploying the application in production, the metadata can be cached, to avoid performance issues.

jaxon()->setOptions([
    'format' => 'attributes',
    'cache' => [
        'enabled' => true,
        'dir' => '/path/to/the/cache/dir',
    ],
], 'core.metadata');

Usage

The following attributes are provided.

#[Jaxon\Attributes\Attribute\Exclude]

It prevents a method or a class from being exported to javascript. It takes an optional boolean parameter.

use Jaxon\Attributes\Attribute\Exclude;

/**
 * #[Exclude(true)]
 */
class JaxonExample
{
// This class will not be exported to javascript.
}
use Jaxon\Attributes\Attribute\Exclude;

class JaxonExample
{
    /**
     * #[Exclude]
     */
    public function doNot()
    {
        // This method will not be exported to javascript.
    }
}

#[Jaxon\Attributes\Attribute\Upload]

It adds file upload to an ajax request. It takes the id of the HTML field as a mandatory option. It applies only to methods.

use Jaxon\Attributes\Attribute\Upload;

class JaxonExample extends \Jaxon\App\Component
{
    /**
     * #[Upload(field: 'div-user-file')]
     */
    public function saveFile()
    {
        // Get the uploaded files.
        $files = $this->upload()->files();
    }
}

#[Jaxon\Attributes\Attribute\Before]

It defines a method of the class as a callback to be called before processing the request. It takes the name of the method as a mandatory parameter, and an array as optional parameters to be passed to the callback. It applies to methods and classes.

use Jaxon\Attributes\Attribute\Before;

class JaxonExample
{
    protected function funcBefore1()
    {
        // Do something
    }

    protected function funcBefore2($param1, $param2)
    {
        // Do something with parameters
    }

    /**
     * #[Before(call: 'funcBefore1')]
     * #[Before(call: 'funcBefore2', with: ['value1', 'value2'])]
     */
    public function action()
    {
    }
}

#[Jaxon\Attributes\Attribute\After]

It defines a method of the class as a callback to be called after processing the request. It takes the name of the method as a mandatory parameter, and an array as optional parameters to be passed to the callback. It applies to methods and classes.

use Jaxon\Attributes\Attribute\After;

class JaxonExample
{
    protected function funcAfter1()
    {
        // Do something
    }

    protected function funcAfter2($param)
    {
        // Do something with parameter
    }

    /**
     * #[After(call: 'funcAfter1')]
     * #[After(call: 'funcAfter2', with: ['value'])]
     */
    public function action()
    {
    }
}

#[Jaxon\Attributes\Attribute\Callback]

It defines a javascript object to be used as callback when processing the ajax request.

It was added in version 2.2.0.

use Jaxon\Attributes\Attribute\Callback;

/**
 * Default callback for all the requests to the class.
 *
 * #[Callback(name: 'jaxon.ajax.callback.example')]
 */
class JaxonExample
{
    /**
     * Specific callback for this method. It is added to the default class callback.
     *
     * #[Callback(name: 'jaxon.ajax.callback.action')]
     */
    public function action()
    {
    }
}

#[Jaxon\Attributes\Attribute\Databag]

It defines a data bag to be appended to ajax requests to a method. It takes the name of the data bag as a mandatory parameter. It applies to methods and classes.

use Jaxon\Attributes\Attribute\Databag;

class JaxonExample extends \Jaxon\App\Component
{
    /**
     * #[Databag(name: 'user')]
     */
    public function action()
    {
        // Update a value in the data bag.
        $count = $this->bag('user')->get('count', 0);
        $this->bag('user')->set('count', $count++);
    }
}

#[Jaxon\Attributes\Attribute\Inject]

It defines an attribute that will be injected in a class.

When applied on methods and classes, it takes the name and the class of the attribute as parameters.

use Jaxon\Attributes\Attribute\Inject;

class JaxonExample extends \Jaxon\App\Component
{
    /**
     * @var \App\Services\Translator
     */
     protected $translator;

    /**
     * #[Inject(attr: 'translator', class: \App\Services\Translator::class)]
     */
    public function translate(string $phrase)
    {
        // The $translator property is set from the DI container when this method is called.
        $phrase = $this->translator->translate($phrase);
    }
}

The class parameter is optional, and can be omitted if it is already specified by a @var attribute.

use Jaxon\Attributes\Attribute\Inject;

class JaxonExample extends \Jaxon\App\Component
{
    /**
     * @var \App\Services\Translator
     */
     protected $translator;

    /**
     * #[Inject(attr: 'translator')]
     */
    public function translate(string $phrase)
    {
        // The $translator property is set from the DI container when this method is called.
        $phrase = $this->translator->translate($phrase);
    }
}

When applied on attributes, it takes the class of the attribute as only parameter, which can be omitted if it is already specified by a @var attribute.

use Jaxon\Attributes\Attribute\Inject;

class JaxonExample extends \Jaxon\App\Component
{
    /**
     * #[Inject(class: \App\Services\Translator::class)]
     * @var \App\Services\Translator
     */
     protected $translator;

    public function translate(string $phrase)
    {
        // The $translator property is set from the DI container when this method is called.
        $phrase = $this->translator->translate($phrase);
    }
}
use Jaxon\Attributes\Attribute\Inject;

class JaxonExample extends \Jaxon\App\Component
{
    /**
     * #[Inject]
     * @var \App\Services\Translator
     */
     protected $translator;

    public function translate(string $phrase)
    {
        // The $translator property is set from the DI container when this method is called.
        $phrase = $this->translator->translate($phrase);
    }
}

统计信息

  • 总下载量: 309
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 1
  • 依赖项目数: 2
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2025-10-20

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固