Skip to content

Latest commit

 

History

History
55 lines (38 loc) · 1.62 KB

ajax.md

File metadata and controls

55 lines (38 loc) · 1.62 KB

Ajax

The Ajax facade simplifies the management of AJAX calls in your Laravel application by providing a simple and configurable abstraction.

The Ajax class allows you to manage AJAX calls using chainable configuration methods.

Example Usage

Here's an example of using the Ajax class with the fluent syntax:

Ajax::listen('my_action', function () {
    // AJAX handling logic here
})->forLoggedUsers();

Instance Creation and Configuration

To start, you can use the listen() method of the Ajax Facade to create an instance and configure the action and associated callback:

Ajax::listen('my_action', function () {
    // AJAX handling logic here
});

Configuration for Target Users

Use the forLoggedUsers() and forGuestUsers() methods to specify the target users for the AJAX call:

Ajax::listen('my_action', function () {
    // AJAX handling logic here
})->forLoggedUsers();
// or
Ajax::listen('my_action', function () {
    // AJAX handling logic here
})->forGuestUsers();

Complete Example

Here's a complete example of using the Ajax class:

$response = Ajax::listen('my_action', function () {
    // AJAX handling logic here
})->forLoggedUsers();

The above example configures an action, a callback, and target users, and then executes the AJAX call. The server response is stored in the $response variable.

Important: Automatic Hook Declaration

By default, this will automatically declare hooks on the WordPress actions wp_ajax_{action} and/or wp_ajax_nopriv_{action}. This ensures the proper routing of the AJAX request based on the user's login status.