Lumen + MongoDB codebase containing real world examples (CRUD, auth, advanced patterns, etc) that adheres to the RealWorld spec and API.
This codebase was created to demonstrate a fully functional REST API built with Lumen + MongoDB, including CRUD operations, authentication, routing, pagination, and more.
It borrows heavily from the excellent Laravel implementation by SandeeshS.
For more information on how this works with other frontends/backends, head over to the RealWorld repo.
Hope you'll find this example helpful. Pull requests are welcome!
Please check the official Lumen installation guide for server requirements before you start. Official Documentation
Clone the repository
git clone git@github.com:elcobvg/lumen-realworld-example-app.git
Switch to the repo folder
cd lumen-realworld-example-app
Install all the dependencies using composer
composer install
Copy the example env file and make the required configuration changes in the .env file
cp .env.example .env
Generate a new application key
Since Lumen doesn't have the php artisan key:generate
command, there's a custom route http://localhost:8000/appKey
to help you generate an application key.
Generate a new JWT authentication secret key
php artisan jwt:secret
Run the database migrations (Set the database connection in .env before migrating)
php artisan migrate
Start the local development server
php -S localhost:8000 -t public
You can now access the server at http://localhost:8000
TL;DR command list
git clone git@elcobvg/lumen-realworld-example-app.git
cd lumen-realworld-example-app
composer install
cp .env.example .env
php artisan key:generate
php artisan jwt:secret
Make sure you set the correct database connection information before running the migrations Environment variables
php artisan migrate
php -S localhost:8000 -t public
Populate the database with seed data with relationships which includes users, articles, comments, tags, favorites and follows. This can help you to quickly start testing the api or couple a frontend and start using it with ready content.
Run the database seeder and you're done
php artisan db:seed
Note : It's recommended to have a clean database before seeding. You can refresh your migrations at any point to clean the database by running the following command
php artisan migrate:refresh
This application adheres to the api specifications set by the Thinkster team. This helps mix and match any backend with any other frontend without conflicts.
More information regarding the project can be found here https://github.com/gothinkster/realworld
- laravel-mongodb - MongoDB based Eloquent model and Query builder
- jwt-auth - For authentication using JSON Web Tokens
- laravel-cors - For handling Cross-Origin Resource Sharing (CORS)
app/Models
- Contains all the Eloquent modelsapp/Http/Controllers
- Contains all the api controllersapp/Http/Middleware
- Contains the JWT auth middlewareapp/Providers
- Contains the JWT auth service providerapp/RealWorld/Favorite
- Contains the files implementing the favorite featureapp/RealWorld/Filters
- Contains the query filters used for filtering api requestsapp/RealWorld/Follow
- Contains the files implementing the follow featureapp/RealWorld/Paginator
- Contains the pagination class used to paginate the resultapp/RealWorld/Slug
- Contains the files implementing slugs to articlesconfig
- Contains all the application configuration filesdatabase/factories
- Contains the model factory for all the modelsdatabase/migrations
- Contains all the database migrationsdatabase/seeds
- Contains the database seederroutes
- Contains all the api routes defined in web.php filetests
- Contains all the application teststests/Feature/Api
- Contains all the api tests
.env
- Environment variables can be set in this file
Note : You can quickly set the database information and other variables in this file and have the application fully working.
Run the Lumen development server
php -S localhost:8000 -t public
The api can now be accessed at
http://localhost:8000/api
Request headers
Required | Key | Value |
---|---|---|
Yes | Content-Type | application/json |
Yes | X-Requested-With | XMLHttpRequest |
Optional | Authorization | Token {JWT} |
Refer the api specification for more info.
This applications uses JSON Web Token (JWT) to handle authentication. The token is passed with each request using the Authorization
header with Token
scheme. The JWT authentication middleware handles the validation and authentication of the token. Please check the following sources to learn more about JWT.
This applications has CORS enabled by default on all API endpoints. The CORS allowed origins can be changed by setting them in the config file. Please check the following sources to learn more about CORS.