PHPackages                             josegonzalez/cakephp-ajax-controller - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. josegonzalez/cakephp-ajax-controller

AbandonedCakephp-plugin[Utility &amp; Helpers](/categories/utility)

josegonzalez/cakephp-ajax-controller
====================================

Automatically support ajax requests from popular Javascript libraries without any more thought.

373PHP

Since Nov 29Pushed 10y ago1 watchersCompare

[ Source](https://github.com/josegonzalez/cakephp-ajax-controller)[ Packagist](https://packagist.org/packages/josegonzalez/cakephp-ajax-controller)[ RSS](/packages/josegonzalez-cakephp-ajax-controller/feed)WikiDiscussions master Synced 2mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

[![Build Status](https://camo.githubusercontent.com/f1af27a3be1d57f3de66ef8728dd87870e90e0d61f1fb96591d7a56af721a7b1/68747470733a2f2f7472617669732d63692e6f72672f6a6f7365676f6e7a616c657a2f63616b657068702d616a61782d636f6e74726f6c6c65722e706e673f6272616e63683d6d6173746572)](https://travis-ci.org/josegonzalez/cakephp-ajax-controller) [![Coverage Status](https://camo.githubusercontent.com/28f93ac05a79091afab303289bf3f6487104ae7fba834032f1c322c6402f5fea/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6a6f7365676f6e7a616c657a2f63616b657068702d616a61782d636f6e74726f6c6c65722f62616467652e706e673f6272616e63683d6d6173746572)](https://coveralls.io/r/josegonzalez/cakephp-ajax-controller?branch=master) [![Total Downloads](https://camo.githubusercontent.com/2a57ea1db362190e4bdbac36d90edc53cd15221b1ec0f5aa6d266f9792c9747d/68747470733a2f2f706f7365722e707567782e6f72672f6a6f7365676f6e7a616c657a2f63616b657068702d616a61782d636f6e74726f6c6c65722f642f746f74616c2e706e67)](https://packagist.org/packages/josegonzalez/cakephp-ajax-controller) [![Latest Stable Version](https://camo.githubusercontent.com/a80cec1b5decc90327f288f54ae230b1c66fbe6110b6e02b3b4974f1c97bba78/68747470733a2f2f706f7365722e707567782e6f72672f6a6f7365676f6e7a616c657a2f63616b657068702d616a61782d636f6e74726f6c6c65722f762f737461626c652e706e67)](https://packagist.org/packages/josegonzalez/cakephp-ajax-controller)

AjaxController
==============

[](#ajaxcontroller)

Automatically support ajax requests from popular Javascript libraries without any more thought.

Background
----------

[](#background)

In the process of writing a new tutorial for CakePHP 2.0, I realized some of the stuff I was writing could be abstracted into it's own self-contained class. I did so, and moved the logic into the AppController.

Recently I was working on a side-project that would need ajax-enabled JSON responses in CakePHP 1.3. With my previous experience, I knew doing this was possible, but rather than copy-pasting the same code over and over, I rewrote it into a plugin for future reuse and abuse.

Requirements
------------

[](#requirements)

- CakePHP 2.x
- PHP 5.3

Installation
------------

[](#installation)

*\[Using [Composer](http://getcomposer.org/)\]*

Add the plugin to your project's `composer.json` - something like this:

```
{
  "require": {
      "josegonzalez/cakephp-ajax-controller": "dev-master"
  }
}

```

Because this plugin has the type `cakephp-plugin` set in it's own `composer.json`, composer knows to install it inside your `/Plugins` directory, rather than in the usual vendors file. It is recommended that you add `/Plugins/AjaxController` to your .gitignore file. (Why? [read this](http://getcomposer.org/doc/faqs/should-i-commit-the-dependencies-in-my-vendor-directory.md).)

*\[Manual\]*

- Download this:
- Unzip that download.
- Copy the resulting folder to `app/Plugin`
- Rename the folder you just copied to `AjaxController`

*\[GIT Submodule\]*

In your app directory type:

git submodule add -b master git://github.com/josegonzalez/cakephp-ajax-controller.git Plugin/AjaxController git submodule init git submodule update

*\[GIT Clone\]*

In your `Plugin` directory type:

git clone -b master git://github.com/josegonzalez/cakephp-ajax-controller.git AjaxController

### Enable plugin

[](#enable-plugin)

In 2.0 you need to enable the plugin your `app/Config/bootstrap.php` file:

CakePlugin::load('AjaxController');

If you are already using `CakePlugin::loadAll();`, then this is not necessary.

Usage
-----

[](#usage)

Place the following code in your `app/app_controller.php`:

```
App::import('Lib', 'AjaxController.AjaxController');
class AppController extends AjaxController {
}

```

When making Ajax Requests that prefer JSON, the `AjaxController` will automatically convert the entire response into json. Please note that this also includes the view variables that would normally be set. If you'd like to hide any data, do not set it for the view.

This also handles all redirects properly, so your ajax request will be able to perform as a regular request might.

In order to disable JSON responses for Ajax requests, you can either set `$this->_disableAjax = true;` or call `$this->_disableAjax()` with an array of actions to disable JSON responses for.

```
App::import('Lib', 'AjaxController.AjaxController');
class AppController extends AjaxController {
}
class PostsController extends AppController {
    function beforeFilter() {
        $this->_disableAjax('index', 'view');
    }

    function edit() {
        $this->_disableAjax = true;
    }
}

```

TODO:
-----

[](#todo)

- Unit Tests
- Provide XML responses when desired

License
-------

[](#license)

The MIT License (MIT)

Copyright (c) 2011 Jose Diaz-Gonzalez

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

###  Health Score

22

—

LowBetter than 22% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity10

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity41

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://www.gravatar.com/avatar/f38b500117ee9754ea7a4c56b800c8151ca0605b1715a3b2ff054aae504f7393?d=identicon)[josegonzalez](/maintainers/josegonzalez)

---

Top Contributors

[![josegonzalez](https://avatars.githubusercontent.com/u/65675?v=4)](https://github.com/josegonzalez "josegonzalez (9 commits)")

### Embed Badge

![Health badge](/badges/josegonzalez-cakephp-ajax-controller/health.svg)

```
[![Health](https://phpackages.com/badges/josegonzalez-cakephp-ajax-controller/health.svg)](https://phpackages.com/packages/josegonzalez-cakephp-ajax-controller)
```

###  Alternatives

[palantirnet/drupal-skeleton

A Drupal project template.

141.4k](/packages/palantirnet-drupal-skeleton)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
