Laravel 5.5 API Tutorial: Part 1 – Intro & Project Setup

After searching for examples on how to setup a Laravel 5.5 API project, particularly one that implements a JWT authentication model, I was unfortunately left piecing together various bits and commands from different blog posts and github discussions.  Some of that was due to existing examples using Laravel 5.4; the subtle differences between Laravel 5.4 and 5.5 were enough to require additional research and debugging vs. enjoying a seamless API setup + JWT how-to.  In light of that I wanted to put together a few blog posts on what I found, with the goal of providing readers what I was seeking: a seamless start-to-finish example of implementing an API with JWT authentication in Laravel 5.5.

In this post we’ll only cover setting up a fresh Laraval 5.5 project to serve as ground zero for our API example, and verify the setup with a simple hello world api route. This much is likely more suitable for Laravel newcomers. Subsequent posts will progress through various API steps including user registration, JWT authentication, and modifying data.

Let’s get started.

Creating A New Laravel Project

Creating a new Laravel project is super easy; there’s not much too it thanks to Laravel’s CLI support. Let’s assume a project name of laravel_api.

To start, run the following command from a suitable directory to create our project:

laravel new laravel_api

Second, some boilerplate is required to further initialize our project. In particular, we need to create a suitable .env file, generate an application key, setup database config, etc. The first two can be handled by running the following commands:

cd laravel_api
cp .env.example .env
php artisan key:generate

To setup database config, create a MySQL database and revise the .env file to point to it.  The 4 config entries that’ll need to change are:

DB_HOST=[the db server ip, e.g. 127.0.0.1]
DB_DATABASE=[the name of the database you created]
DB_USERNAME=[a suitable database user name]
DB_PASSWORD=[a suitable database user password]

Third, and to close out this post, let’s test the project setup to ensure everything is wired correctly before proceeding further. Start PHP’s test server for our Laravel project:

php artisan serve

… then visit http://localhost:8000 in a web browser.  You should see the default Laravel splash page (shown below). If not, review or repeat the above steps until any problems are resolved.

 

Hello World API Endpoint

As a quick primer for future posts, let’s setup a hello world API endpoint. This will provide a shallow dive into route setup, testing the api with Postman, and just overall confirm that our project is ready to move forward with subsequent API development.

First, let’s setup our test route. Since we’re building an API, the routes we’ll add will go in the routes/api.php file. Add the following GET route to that file:

Route::middleware('api')->get('/hello_world', function () {
    return json_encode(['message' => 'hello world']);
});

Next, let’s test this endpoint.  You can use your browser, but I’d recommend using and becoming familiar with Postman.  It provides a far more robust API testing platform than using a browser or perhaps manual CURL commands from a terminal.

Once Postman is downloaded, open it up and setup a GET request similar to the following:

Upon clicking Send, you should see a simple JSON response in the bottom pane with the message hello world, which confirms the route we just setup is working.  If any errors were received, review the previous steps until those issues are resolved.

Conclusion

That’s it for this first part.  This won’t be anything too new for Laravel veterans, but for newcomers to the framework, this should help demonstrate how easy it is to setup a new Laravel project and confirm the setup is ready for API development.

From here, the next part will dive further into database setup, creating our first real API endpoint, getting users into the database, and more.

This entry was posted in General, Laravel, PHP. Bookmark the permalink.

One Response to Laravel 5.5 API Tutorial: Part 1 – Intro & Project Setup

  1. su says:

    Nice article. I can understand it easily. Read on, hope I can understand the next two articles.

Leave a Reply

Your email address will not be published. Required fields are marked *