In this tutorial, you will learn how to create a basic REST API using PHP's Laravel framework. Laravel is one of the most popular PHP frameworks for web application development and REST API creation.
Before continuing, you can learn more about REST APIs in general, how they work, their components, etc. in our series of tutorials
Creating an API using this framework is easy, straightforward, and only requires the following basic steps.
To create a simple API using Laravel, we first need to verify that PHP and composer are installed by running the following commands:
To generate a new Laravel application, we will use Composer. It will automatically install the required dependencies and create an application skeleton. To create this new application, run the following command in any folder of your choice:
You can rename the application by modifying
my-api to the name of your choice. Composer will create a new folder containing the scaffolded application.
To add routes to our REST API, we first need to create a new controller. To do so, run the following command:
It will create a new API controller
UsersController in a subfolder of the
./app/Http/Controllers folder. Thanks to the
--api flag, the controller code will be stripped out of unnecessary methods.
Next, we will need to add a new route pointing to our
UsersController. To do so, open the
./routes/api.php file, and add the following lines:
You can verify that your API route were added by listing them using the following command:
You may have more routes showing in this list depending on your configuration.
We now have a basic setup that allows us to serve the application and test our endpoints.
To serve the PHP Laravel application, run the following command:
Your API is available on http://localhost:8000/api/xxxx. You can now check that you are receiving a successful response (status code 200) when calling the
/api/users endpoint with your favorite API testing tool (here Insomnia):
We will not create a Model or connect the controller to a database in this tutorial. We will only return some fake JSON data from the API endpoints.
Let's return a list of users from the
GET /users route by returning a simple PHP object from the controller's
We can also return a JSON message from the
DELETE /users route by returning a simple success message from the controller's
Note that Laravel will automatically handle the JSON serialization of our objects and send the correct
Content-Type: application/json header as we use the
You can do a test call to the following API endpoints
GET /users and
DELETE /users and see the returned response:
Of course, this code is quite basic, and the two routes we created should probably load the list of users and delete users from a database before returning a result. But this is out of the scope of this small tutorial.
Working with an API can be challenging. It could be unavailable for various reasons: the whole API is under development, some routes are missing, the documentation is outdated, the access is restricted to the production environment or behind a firewall, etc.
Instead of waiting for the API to be ready to be able to consume it, you could mock it using an API mocking tool like Mockoon.
API mocking is a technique that consists in imitating an unavailable API by simulating the endpoints and their responses. With this technique, you can have a running mock in no time and start calling it right away from your front-end or back-end application.
Mocking an API with Mockoon is easy and requires only some small steps to start working.
To learn more about setting up Mockoon and creating your first fake API in less than 5 minutes, head over to our getting start tutorial
API mocking with Mockoon