intpp/yii-ajax-action 问题修复 & 功能扩展

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

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

intpp/yii-ajax-action

最新稳定版本:1.0.10

Composer 安装命令:

composer require intpp/yii-ajax-action

包简介

Yii action for ajax methods

README 文档

README

Usage:

  1. Create class for ajax actions (ex.: SiteAjaxAction)
namespace your\namespace\here;

use intpp\yii\actions\AjaxAction;

class SiteAjaxAction extends AjaxAction
{
    /**
     * @param string $name
     * @param int $age
     * @param string $gender
     * @param array $hobbies
     */
    public function getFormattedInfo($name, $age, $gender = 'male', array $hobbies)
    {
        $this->setOutput('result', true);
        $this->setOutput('text', implode(', ', [
            'Your name is ' . $name,
            'age: ' . $age,
            'gender: ' . $gender,
            'hobbies: ' . implode(', ', $hobbies)
        ]));
    }
}
  1. Include in your controller:
    public function actions()
    {
        return [
            // Other included actions
            'ajax' => 'your\namespace\here\SiteAjaxAction',
        ];
    }
  1. Use in your JS applications:

3.1 In your view or layout file set JS variable with url to ajax action, for example:

    $ajaxUrl = Yii::app()->createUrl('/site/ajax');
    Yii::app()->clientScript->registerScript('ajaxUrl', "var ajaxUrl='{$ajaxUrl}';");

3.2 In your JS application you use that variable for ajax requests, for example:

    $(document).on('click', 'a#getInfo', function() {
        $.ajax({
            url: ajaxUrl,
            data: {
                method: 'getFormattedInfo', // <---- Name of a function in your AjaxAction class
                name: 'Vasya', age: 12, hobbies: ['sport', 'dev', 'sex'] // <---- Parameters for the function
            },
            dataType: 'JSON',
            success: function(response) {
                if(response.result === true) {
                    alert(response.text);
                }
            }
        });
    });

P.S: Other examples in examples directory =)

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: BSD
  • 更新时间: 2014-11-19

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固