Download the PHP package cyberpunkcodes/laravel-ajax-json-api without Composer

On this page you can find all versions of the php package cyberpunkcodes/laravel-ajax-json-api. 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 laravel-ajax-json-api

Laravel AJAX JSON API

Turn your Laravel application into an AJAX JSON API.

Installation

Install using Composer:

This package will automatically be injected to your api middleware thanks to the Laravel ServiceProvider via:

You do not have to do anything other than use/apply the api middleware. The above lines are just to show you how the magic happens, by prepending ForceJson and AjaxOnly to the api middleware group, and adjusting their priority to be first.

Other than installing with Composer, all you have to do is use the api middleware. This could be in your Controller or wherever you need.

Ideally, in a sole AJAX JSON API, it would be applied globally to the entire application. This would be done in your app/Providers/RouteServiceProvider.php.

Example RouteServiceProvider.php:

The default for a Laravel 10 application is:

Which we change to:

This package will work with the default implementation since it also uses the api middleware. However, it is recommended to use versioning so you can create a v2, v3, and so on as needed for future upgrades. So lets setup versioning.

You should use the example RouteServiceProvider as shown above.

Then create an api directory in the routes folder. Move and rename the /routes/api.php file to /routes/api/v1.php.

/routes/api/v1.php should look like:

Notice the / path returns an array containing home. This path would be: https://example.com/v1/ which would return a JSON response:

This is just to get you started so you can make your first request with Postman or whatever you use, and to confirm you are receiving JSON responses back.

You should be making an AJAX request by including the following headers:

You also need to create the API Controller. Create an Api directory in app/Controllers. You can delete the default app/Controllers/Controller.php if you aren't going to handle frontend web requests (ie: if it is solely an API).

Create a controller at app/Controllers/Api/Controller.php:

Making an AJAX request to https://example.com/v1/test will return a JSON response:

The two built in routes/functions/responses are just examples to get you started.

Final Notes

If you return a string, it will return an html response. This is just how Laravel works. You do not have to specifically return a json response. Just return an array and it will return a json response for you.

I leave the / web route (in routes/web.php) and just have it return blank:

This is so if the API is accessed via the web, it just returns a blank page and not an error.


All versions of laravel-ajax-json-api with dependencies

PHP Build Version
Package Version
Requires php Version ^7.2|^8.0
illuminate/support Version ^8.0|^9.0|^10.0
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 cyberpunkcodes/laravel-ajax-json-api contains the following files

Loading the files please wait ....