Download the PHP package heimrichhannot/contao-ajax without Composer

On this page you can find all versions of the php package heimrichhannot/contao-ajax. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package contao-ajax

Ajax

Ajax requests within contao are not centralized by default. Due to the handling of ajax requests within different types of modules a simple \Environment::get('isAjaxRequest') is not enough to delegate the request to the related module / method. This module provides a global configuration, where you can attach your module within $GLOBALS['AJAX'] as a custom group and add your actions with the required parameters that should be checked against the request.

Technical instruction

The following section will show you how your custom ajax actions can be registered and triggered.

1. Configuration / Setup

The following example shows the [heimrichhannot/contao-formhybrid] (https://github.com/heimrichhannot/contao-formhybrid) ajax configuration.

As you can see, we have a group formhybrid that delegates all ajax request with this group parameter to formhybrid. Then there are some actions toggleSubpalette, asyncFormSubmit and so on. These mehtod must be present with the same name in the delegated context. You can provide arguments, that should be called within the function and added as arguments to the method. If the argument is optional, than the request will be valid if the argument is not present, otherwise all arguments must be present in the request, to have a valid ajax request. If you want to protect the ajax request against cross site violations, than add csrf_protection => true to your configuration and dont forget to update the ajax url on each request!

2. How can i create the url to my ajax action?

We provide a simple helper method within HeimrichHannot\Ajax\AjaxAction that is called generateUrl. The following example shows, how we create the toggleSubpalette url within FormHybrid.

As you can see, we do not add the arguments by default. This is done within the associated javascript code for toggleSubpalette. You have to check within your javascript code, that the arguments subId, subField, subLoad are provided as $_POST parameters within the ajax request by your own.

3. How are my ajax actions triggered?

The give you the biggest possible freedom, we decided to be always within contao context. Therefore all request preconditions are checked within the default contao request-cycle.

In case of the toggleSubpalette example we trigger the action within DC_Hybrid::__construct() and provide a new FormAjax($this) as Response Context.

CAUTION: Don't call "runActiveAction" in generate() of a Contao module since that's too late. It's always best to run it in __construct().

So what is done here?

  1. The ajax action is requested from your javascript code.
  2. The ajax action is delegated to the current page, where our DC_Hybrid extended Module is available.
  3. The Ajax Controller will check the request against the given parameters for toggleSubpalette from the $GLOBALS['AJAX'] config.
  4. If all requirements were meet, and the method toggleSubpalette is available within the given context new FormAjax($this), the method is trigged with the given arguments.
  5. Now you can do your module related stuff within toggleSubpalette
  6. If you want to return data to the ajax request, then you have to return a valid HeimrichHannot\Ajax\Response\Response Object.
  7. The returned HeimrichHannot\Ajax\Response\Response Object will be converted to a JSON Object and the request will end here.

Response Objects

Currently we implemented three response objects.

  1. HeimrichHannot\Ajax\Response\ResponseSuccess
  2. HeimrichHannot\Ajax\Response\ResponseError
  3. HeimrichHannot\Ajax\Response\ResponseRedirect

ResponseSuccess

This will return a JSON Object with the HTTP-Statuscode HTTP/1.1 200 OK to the ajax action.

Example:

Client-Side:
Server-Side:

ResponseError

This will return a JSON Object with the HTTP-Statuscode HTTP/1.1 400 Bad Request to the ajax action.

Example:

Client-Side:
Server-Side:

ResponseRedirect

This will return a JSON Object with the HTTP-Statuscode HTTP/1.1 301 Moved Permanently to the ajax action. The redirect url is provided within the xhr response object result.data.url;

Example:

Client-Side:
Server-Side:

Unit Testing

For unit testing, define the variable UNIT_TESTING as true within the $GLOBALS.

Than you are able to catch the ajax result within you test, by catching the HeimrichHannot\Ajax\Exception\AjaxExitException.


All versions of contao-ajax with dependencies

PHP Build Version
Package Version
Requires php Version ~5.4 || ~7.0 || ^8.0
contao/core-bundle Version ^3.5.1 || ~4.1
contao-community-alliance/composer-plugin Version ~2.4 || ~3.0
heimrichhannot/contao-haste_plus Version ^1.6
heimrichhannot/contao-request Version ^1.1
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package heimrichhannot/contao-ajax contains the following files

Loading the files please wait ....