Thursday 13 June 2024

Create a basic API using Laravel 11

This post is specific to Laravel 11, which has a sightly different way of setting up it's API to previous versions.

Pre-requisites

I'll assume that you have Laravel set up to use laravel commands.

Make sure you have SQLite installed.

Basic Steps

Run:

laravel new basic-test

To question 'Would you like to install a starter kit?', select 'No starter kit'.

To question 'Which testing framework do you prefer?',  select 'Pest'.

To question 'Would you like to initialize a Git repository?', select 'yes'.

To question 'Which database will your application use?', select 'SQLite'.

To question 'Would you like to run the default database migrations?', select 'yes'.

To question 'Would you like to create it?', select 'yes'.

Run:

cd basic-test

Run:

php artisan install:api

Within routes/api.php, add the following lines below the 'use' statements:

Route::get('/', function () {

    return response()->json(['message' => 'Hello World']);

});

Run:

php artisan serve

Open a web browser, and add http://127.0.0.1:8000/api/ to the address bar.

You should see some JSON showing the message 'Hello world'.

No comments:

Post a Comment