Install Laravel and Other Dependencies
Let’s cover the steps to install Laravel and other dependencies in your local environment.
Prerequisites for installing and developing APIs in Laravel
Before diving into installing Laravel and developing APIs, ensure you have the following prerequisites in place:
Basic knowledge of PHP and Laravel: Familiarity with PHP programming is essential since Laravel is a PHP framework.
Local development environment setup: A local development environment like XAMPP, MAMP or Docker which should include:
PHP: Laravel requires PHP 8.0 or higher.
A web server: Apache or Nginx is recommended.
Database: MySQL, PostgreSQL, SQLite, or SQL Server.
Version control system: A version control system like Git to manage the codebase.
Text editor or IDE: A text editor like Visual Studio Code, PHPStorm, or Sublime Text that supports PHP and Laravel development.
Step 1: Install Laravel
There are multiple ways to create a new Laravel application. No matter which one you pick, you end up with a clone of the skeleton repository with composer dependencies installed.
With the global Laravel installer, it’s just one command: laravel new example-app
You are free to use the method that suits you to complete the installation.
After that, you should set environment variables in the .env
file, specifically the App URL and DB connection details. You should be able to open the homepage in the browser to be ready for the next step.
Step 2: Tidy up a couple of things
Learn how to clean up a newly created Laravel application that is created from a template skeleton. This gives you a strong foundation for the new app.
The first thing that you should do is remove everything from the README.md
file. It should contain the details about the project, the steps to install and set it up on a new computer, and other important notes for your future self and other team members.
This Laravel web app will serve only the API endpoints and not the actual GUI to let users interact with it. Therefore, we highly suggest you mention that in the resources/views/welcome.blade.php
file, which is shown to the user when they open the homepage in the browser. It could be as simple as the following text so that there is no confusion by the developers or even the client.
This is an API-only project and does not offer any frontend.
Step 3: Install Laravel Sanctum
We will be using Laravel Sanctum for the authentication of the API requests. Many developers get confused when it comes to API authentication because Laravel used to provide the ‘shortcuts’ mainly for the session-based web app authentication. There was little official documentation to tackle authentication for APIs. There was also Laravel Passport, which kind of supported it despite being created for another purpose.
That is why Taylor Otwell, the creator of Laravel, created this official package. It helps with two things: SPA authentication and API Token Authentication. Our project is going to offer token-based authentication.
Please go ahead and follow the installation instructions for Laravel Sanctum. Here are the commands you need to fire.
composer require laravel/sanctum
php artisan vendor:publish --provider="Laravel\Sanctum\SanctumServiceProvider"
php artisan migrate
And then update the app/Http/Kernel.php
file to add Sanctum’s middleware to your ‘api middleware’ group.
'api' => [
\Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful::class,
'throttle:api',
\Illuminate\Routing\Middleware\SubstituteBindings::class,
],
Congratulations, your Laravel web app is now ready to authenticate users.
Step 4: Install CSV seeder
We used CSVs to seed the country’ and state’ data. You may use the following command to install it:
composer require flynsarmy/csv-seeder
More details of the package can be found here.
Step 5: Install Laravel model states
We used this package to manage the state of the restaurant tables. You may use the following command to install it:
composer require spatie/laravel-model-states
More details of the package can be found here.