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 JWT for authentication, 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.

Update: before creating the project, be sure the following php packages are installed, otherwise you’ll receive errors due to them missing when the project is created:

sudo apt-get install -y php-zip php-mbstring php-xml

 

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

laravel new laravel_api

Second, some boilerplate is required to further initialize the 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, 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 confirm our project is ready to move forward into further 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 the end of 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 more robust API testing platform than using a browser or CURL commands form a terminal.

Once Postman is downloaded, open it 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, confirming the route we setup is working.  If any errors were received, review the previous steps until those issues are resolved; also be sure the web server (that was started above via php atrisan serve) is still running.

Conclusion

That’s it for part 1.  This wasn’t anything 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.

Next, we’ll dive 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 *