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.
Download cyberpunkcodes/laravel-ajax-json-api
More information about cyberpunkcodes/laravel-ajax-json-api
Files in cyberpunkcodes/laravel-ajax-json-api
Package laravel-ajax-json-api
Short Description Laravel AJAX JSON API
License MIT
Homepage https://github.com/CyberPunkCodes/laravel-flashmessages
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.