Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
H

hydra

> 后端框架
Open source

Hydra is a zero-config API boilerplate with Laravel 10x + Laravel Sanctum that comes with an excellent user and role management API out of the box

932 stars0 likes5 views
WebsiteGitHub

About

Hydra is a zero-config API boilerplate with Laravel 10x + Laravel Sanctum that comes with an excellent user and role management API out of the box

Hydra - Zero Config API Boilerplate with Laravel Sanctum

Hydra is a zero-config API boilerplate with Laravel Sanctum and comes with excellent user and role management API out of the box. Start your next big API project with Hydra, focus on building business logic, and save countless hours of writing boring user and role management API again and again.

Hydra works with Laravel 10.x and Sanctum 3.x. If you want to work with Laravel 9.x then checkout the 9.x branch.

  • Hydra - Zero Config API Boilerplate with Laravel Sanctum
    • Getting Started
      • Without Docker (Simple)
      • Using Docker & Laravel Sail
      • Screencast
    • Database Migration and Seeding
    • List of Default Routes
    • Default Roles
    • Routes Documentation
      • User Registration
      • User Authentication/Login (Admin)
      • User Authentication/Login (Other Roles)
      • List Users (Admin Ability Required)
      • Update a User (User/Admin Ability Required)
      • Delete a User (Admin Ability Required)
      • List Roles (Admin Ability Required)
      • Add a New Role (Admin Ability Required)
      • Update a Role (Admin Ability Required)
      • Delete a Role (Admin Ability Required)
      • List Available Roles of a User (Admin Ability Required)
      • Assign a Role to a User (Admin Ability Required)
      • Delete a Role from a User (Admin Ability Required)
    • Notes
      • Default Admin Username and Password
      • Default Role for New Users
      • Single Session or Multiple Session
      • Add Accept: application/json Header In Your API Calls (Important)
      • Logging
      • Code Formatting
    • Tutorial
      • Create a New API Controller
      • Add a Function
      • Create Protected Routes
      • Test Protected Routes
      • Protect a Route with Laravel Sanctum's Ability and Abilities Middleware

Getting Started

It's super easy to get Hydra up and running.

First clone the project and change the directory

bash
git clone https://github.com/hasinhayder/hydra.git
cd hydra

Then follow the process using either Docker or without Docker (simple).

Without Docker (Simple)

  1. Install the dependencies
bash
composer install
  1. Copy .env.example to .env
bash
cp .env.example .env
  1. Generate application key
bash
php artisan key:generate
  1. Start the webserver
bash
php artisan serve

That's mostly it! You have a fully running laravel installation with Sanctum, all configured.

Using Docker & Laravel Sail

  1. Install the dependencies
bash
docker run --rm \
    -u "$(id -u):$(id -g)" \
    -v $(pwd):/var/www/html \
    -w /var/www/html \
    laravelsail/php81-composer:latest \
    composer install --ignore-platform-reqs
  1. Copy .env.example to .env
bash
cp .env.example .env
  1. Run the containers
bash
./vendor/bin/sail up
  1. Generate application key
bash
./vendor/bin/sail artisan key:generate

To learn more about Sail, visit the official Doc.

Screencast

Database Migration and Seeding

Open your .env file and change the DATABASE options. You can start with SQLite by following these steps

  1. Create a new SQLite database
bash
touch database/hydra.sqlite

Or simply create a new file as hydra.sqlite inside your database folder.

  1. You can run both migrations and seeders together by simply running the following command
bash
php artisan migrate:fresh --seed

OR

you can run them separately using the following commands

  1. Run Migrations
bash
php artisan migrate:fresh

Now your database has essential tables for user and roles management.

  1. Run Database Seeders

Run db:seed, and you have your first admin user, some essential roles in the roles table, and the relationship correctly setup.

bash
php artisan db:seed

Please note that the default admin user is [email protected] and the default password is hydra. You should create a new admin user before deploying to production and delete this default admin user. You can do that using the available Hydra user management API or any DB management tool.

List of Default Routes

Here is a list of default routes. Run the following artisan command to see this list in your terminal.

bash
php artisan route:list

Default Roles

Hydra comes with these super-admin,admin,editor,customer & user roles out of the box. For details, open the roles table after database seeding, or open the laravel tinker and experiment with the Role model.

bash
php artisan tinker

run the following command

php
>>> Role::select(['id','slug','name'])->get()
//or
>>> Role::all(['id','name','slug'])
//or
>>> Role::all()

Routes Documentation

Let's have a look at what Hydra has to offer. Before experimenting with the following API endpoints, run your Hydra project using php artisan serve command. For the next part of this documentation, we assumed that Hydra is listening at http://localhost:8000

User Registration

You can make an HTTP POST call to create/register a new user to the following endpoint. Newly created users will have the user role by default.

bash
http://localhost:8000/api/users

API Payload & Response

You can send a Form Multipart payload or a JSON payload like this.

json
{
    "name":"Hydra User",
    "email":"[email protected]",
    "password":"Surprisingly A Good Password"
}

Voila! Your user has been created and is now ready to log in!

If this user already exists, then you will receive a 409 Response like this

json
{
    "error": 1,
    "message": "user already exists"
}

User Authentication/Login (Admin)

Remember Hydra comes with the default admin user? You can log in as an admin by making an HTTP POST call to the following route.

bash
http://localhost:8000/api/login

API Payload & Response

You can send a Form Multipart or a JSON payload like this.

json
{
    "email":"[email protected]",
    "password":"hydra"
}

You will get a JSON response with user token. You need this admin token for making any call to other routes protected by admin ability.

json
{
    "error": 0,
    "token": "1|se9wkPKTxevv9jpVgXN8wS5tYKx53wuRLqvRuqCR"
}

For any unsuccessful attempt, you will receive a 401 error response.

json
{
    "error": 1,
    "message": "invalid credentials"
}

User Authentication/Login (Other Roles)

You can log in as a user by making an HTTP POST call to the following route

bash
http://localhost:8000/api/login

API Payload & Response

You can send a Form Multipart or a JSON payload like this

json
{
    "email":"[email protected]",
    "password":"Surprisingly A Good Password"
}

You will get a JSON response with user token. You need this user token for making any calls to other routes protected by user ability.

json
{
    "error": 0,
    "token": "2|u0ZUNlNtXgdUmtQSACRU1KWBKAmcaX8Bkhd2xVIf"
}

For any unsuccessful attempt, you will receive a 401 error response.

json
{
    "error": 1,
    "message": "invalid credentials"
}

List Users (Admin Ability Required)

To list the users, make an HTTP GET call to the following route, with Admin Token obtained from Admin Login. Add this token as a standard Bearer Token to your API call.

bash
http://localhost:8000/api/users

API Payload & Response

No payload is required for this call.

You will get a JSON response with all users available in your project.

json
[
    {
        "id": 1,
        "name": "Hydra Admin",
        "email": "[email protected]"
    },
    {
        "id": 2,
        "name": "Test User",
        "email": "[email protected]"
    },
]

For any unsuccessful attempt or wrong token, you will receive a 401 error response.

json
{
    "message": "Unauthenticated."
}

Update a User (User/Admin Ability Required)

Make an HTTP PUT request to the following route to update an existing user. Replace {userId} with actual user id. You must include a Bearer token obtained from User/Admin authentication. A bearer admin token can update any user. A bearer user token can only update the authenticated user by this token.

bash
http://localhost:8000/api/users/{userId}

For example, to update the user with id 3, use this endpoint http://localhost:8000/api/users/3

API Payload & Response

You can include name or email, or both in a URL Encoded Form Data or JSON payload, just like this

json
{
    "name":"Captain Cook",
    "email":"[email protected]"
}

You will receive the updated user if the bearer token is valid.

json
{
    "id": 3,
    "name": "Captain Cook",
    "email": "[email protected]",
}

For any unsuccessful attempt with an invalid token, you will receive a 401 error response.

json
{
    "error": 1,
    "message": "invalid credentials"
}

If a bearer user token attempts to update any other user but itself, a 409 error response will be delivered

json
{
    "error": 1,
    "message": "Not authorized"
}

For any unsuccessful attempt with an invalid user id, you will receive a 404 not found error response. For example, when you are trying to delete a non-existing user with id 16, you will receive the following response.

json
{
    "error": 1,
    "message": "No query results for model [App\\Models\\User] 16"
}

Delete a User (Admin Ability Required)

To delete an existing user, make a HTTP DELETE request to the following route. Replace {userId} with actual user id

bash
http://localhost:8000/api/users/{userId}

For example to delete the user with id 2, use this endpoint http://localhost:8000/api/users/2

API Payload & Response

No payload is required for this call.

If the request is successful and the bearer token is valid, you will receive a JSON response like this

json
{
   "error": 0,
   "message": "user deleted"
}

You will receive a 401 error response for any unsuccessful attempt with an invalid token.

json
{
    "error": 1,
    "message": "invalid credentials"
}

For any unsuccessful attempt with an invalid user id, you will receive a 404 not found error response. For example, you will receive the following response when you try to delete a non-existing user with id 16.

json
{
   "error": 1,
   "message": "No query results for model [App\\Models\\User] 16"
}

List Roles (Admin Ability Required)

To list the roles, make an HTTP GET call to the following route, with Admin Token obtained from Admin Login. Add this token as a standard Bearer Token to your API call.

bash
http://localhost:8000/api/roles

API Payload & Response

No payload is required for this call.

You will get a JSON response with all the

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

PHPlaravellaravel-boilerplatelaravel-frameworklaravel-sanctum

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category后端框架
PricingOpen source

> Related tools

N
Node.js
基于 V8 的 JavaScript 运行时
D
Django
Python 高级 Web 框架
S
Spring Boot
Java 生态主流微服务框架