2017 © Pedro Peláez
 

library yii-ajax-action

Yii action for ajax methods

image

intpp/yii-ajax-action

Yii action for ajax methods

  • Tuesday, August 18, 2015
  • by intpp
  • Repository
  • 1 Watchers
  • 1 Stars
  • 155 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 13 Versions
  • 4 % Grown

The README.md

Yii Ajax Action

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:, (*1)

    $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:, (*2)

    $(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 =), (*3)

The Versions

18/08 2015

dev-master

9999999-dev

Yii action for ajax methods

  Sources   Download

BSD

The Requires

  • php >=5.3.0

 

The Development Requires

by Avatar intpp

18/08 2015

1.0.10

1.0.10.0

Yii action for ajax methods

  Sources   Download

BSD

The Requires

  • php >=5.3.0

 

The Development Requires

by Avatar intpp

18/05 2015

1.0.9

1.0.9.0

Yii action for ajax methods

  Sources   Download

BSD

The Requires

  • php >=5.3.0

 

The Development Requires

by Avatar intpp

30/04 2015

1.0.8

1.0.8.0

Yii action for ajax methods

  Sources   Download

BSD

The Requires

  • php >=5.3.0

 

The Development Requires

by Avatar intpp

28/04 2015

1.0.7

1.0.7.0

Yii action for ajax methods

  Sources   Download

BSD

The Requires

  • php >=5.3.0

 

The Development Requires

by Avatar intpp

28/04 2015

1.0.6

1.0.6.0

Yii action for ajax methods

  Sources   Download

BSD

The Requires

  • php >=5.3.0

 

The Development Requires

by Avatar intpp

28/04 2015

1.0.5

1.0.5.0

Yii action for ajax methods

  Sources   Download

BSD

The Requires

  • php >=5.3.0

 

The Development Requires

by Avatar intpp

28/04 2015

1.0.4

1.0.4.0

Yii action for ajax methods

  Sources   Download

BSD

The Requires

  • php >=5.3.0

 

The Development Requires

by Avatar intpp

28/04 2015

1.0.3

1.0.3.0

Yii action for ajax methods

  Sources   Download

BSD

The Requires

  • php >=5.3.0

 

The Development Requires

by Avatar intpp

28/04 2015

1.0.2

1.0.2.0

Yii action for ajax methods

  Sources   Download

BSD

The Requires

  • php >=5.3.0

 

The Development Requires

by Avatar intpp

12/12 2014

v1.0.1

1.0.1.0

Yii action for ajax methods

  Sources   Download

BSD

The Requires

  • php >=5.3.0

 

The Development Requires

by Avatar intpp

21/11 2014

v1.0.0

1.0.0.0

Yii action for ajax methods

  Sources   Download

BSD

The Requires

  • php >=5.3.0

 

The Development Requires

by Avatar intpp

21/11 2014

v1.0.0-rc

1.0.0.0-RC

Yii action for ajax methods

  Sources   Download

BSD

The Requires

 

by Avatar intpp