Laravel 11 + Breeze + Auth API: Adding an API Route To Laravel 11 Project
Laravel v.11 came out with two notable changes: (1) SQLite becomes the default database, and (2) the API route is removed.
Laravel 11 + Breeze + Auth API: Adding an API Route To Laravel 11 Project
Laravel v.11 came out with two notable changes: (1) SQLite becomes the default database, and (2) the API route is removed.
The former is interesting as it makes the project folder more portable. However, the latter means that API developers now have extra work to do.
This article will demonstrate the creation of a Laravel v.11 project using Breeze and how to add an API route to the project.
[1] Create new project
In the Quick app dialog box, type lara11breeze.
Laragon will download Laravel v.11.0.0
Laravel v.11.0 uses sqlite as the default database.
When project creation is done, browse the page to check that the server is live.
http://lara11breeze.test
or
http://localhost/lara11breeze/public
[2] Install Breeze Package
[2.1] Get package
Run composer command:
composer require laravel/breeze:2.0.0
laravel/breeze v2.0.0 will be downloaded into the project folder.
[2.2] Install Package
Run Artisan command:
php artisan breeze:install
Specify the following settings:
BladestackNofor dark mode support1for PHPUnit testing framework
Output:
Finally…:
Check that Breeze has been scaffolded successfully by verifying that the Login and Register links exist.
Click the Register link and ensure that the Register page exists.
Click the Login link and ensure that the Login page exists.
[2.3] Implement Must Verify Email feature
- Activate the use statement for MustVerifyEmail:
use Illuminate\Contracts\Auth\MustVerifyEmail;
- Implements MustVerifyEmail features:
class User extends Authenticatable implements MustVerifyEmail
Edit File C:\laragon\www\lara11breeze\app\Models\User.php :

[2.4] Run migration
Run Artisan command:
php artisan migrate
Laravel will let you know if there is nothing to migrate.
[2.5] Test Login/Register
Warning: before testing, make sure that … You have entered details for mail configuration.
Edit File: C:\laragon\www\lara11breeze\.env :
MAIL_MAILER=smtp
MAIL_HOST=mail.abcdef.my
MAIL_PORT=465
MAIL_USERNAME=abc@def.my
MAIL_PASSWORD=abcd1234
MAIL_ENCRYPTION=null
MAIL_FROM_ADDRESS="admin@abcdef.my"
MAIL_FROM_NAME="${APP_NAME}"
If mail configuration has been set, Browse lara11breeze.test/register and register a user:
Outcome:
Check mailbox:
Click the link. You shall be forwarded to the dashboard page.
[3] Add API AuthController
[3.1] Create AuthController
Run Artisan command:
php artisan make:controller Api/AuthController
[3.2] Edit AuthController
Edit as follows:
(file →app\Http\Controllers\Api\AuthController.php)
<?php
/* app\Http\Controllers\Api\AuthController.php */
namespace App\Http\Controllers\Api;
use App\Models\User;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Auth;
use Illuminate\Validation\Rules;
use Illuminate\Support\Facades\Hash;
use Illuminate\Auth\Events\Registered;
class AuthController extends Controller
{
//
public function register(Request $request): JsonResponse
{
$request->validate([
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:'.User::class],
'password' => ['required', 'confirmed', Rules\Password::defaults()],
]);
$user = User::create([
'name' => $request->name,
'email' => $request->email,
'password' => Hash::make($request->password),
]);
event(new Registered($user));
$created_user= User::where('email', '=', $request->email)->first();
return response()->json([
'user'=>$created_user,
'stus'=>'registered',
'verified'=>false], 200);
}
public function login(Request $request)
{
if (!Auth::attempt($request->only("email", "password"))) {
return response()->json(
[
"user" => Null,
"message" => "Invalid login details",
"stus" => "failed",
],
200
);
}
$user = User::where("email", $request["email"])->firstOrFail();
$user_loggedin=[
'id' => $user->id,
'email' => $user->email,
'email_verified_at'=> $user->email_verified_at,
'stus'=>'loggedin'
];
if ($user->email_verified_at != Null) {
$token = $user->createToken("auth_token")->plainTextToken;
$user_loggedin['user_token']= $token;
$user_loggedin['token_type']= 'Bearer';
$user_loggedin['verified']= true;
} else {
$user_loggedin['verified']= false;
}
return response()->json(
$user_loggedin,
200
);
}
}
[3.3] Install API Package
Laravel 11 requires API Package (Sanctum) to be installed first.
Run Artisan command:
php artisan install:api
Output:
...
- Installing laravel/sanctum (v4.0.2): Extracting archive
...
INFO Published API routes file.
...
INFO Running migrations.
2024_05_03_232650_create_personal_access_tokens_table ........................................ 11.21ms DONE
...
INFO API scaffolding installed. Please add the [Laravel\Sanctum\HasApiTokens] trait to your User model.
...
[3.4] Enable Has API feature in User Model
[1] Add use Laravel\Sanctum\HasApiTokens;
[2] Add HasApiTokens
(file →app\Models\User.php)
<?php
/* app\Models\User.php */
namespace App\Models;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
class User extends Authenticatable implements MustVerifyEmail
{
use HasFactory, Notifiable, HasApiTokens;
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'name',
'email',
'password',
];
/**
* The attributes that should be hidden for serialization.
*
* @var array<int, string>
*/
protected $hidden = [
'password',
'remember_token',
];
/**
* Get the attributes that should be cast.
*
* @return array<string, string>
*/
protected function casts(): array
{
return [
'email_verified_at' => 'datetime',
'password' => 'hashed',
];
}
}
[3.5] Edit API Route
Step [3.3] has automatically created the API route file.
Now, we need to add AuthController class/methods to the API route:
(File → routes\api.php)
<?php
/* routes\api.php */
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
// Assigning middleware to individual route
//Route::get('/user', function (Request $request) {
// return $request->user();
//})->middleware('auth:sanctum');
use App\Http\Controllers\Api\AuthController;
// Registration route
Route::post('/register', [AuthController::class, 'register']);
// Login route
Route::post('/login', [AuthController::class, 'login']);
// Assigning middleware to group of routes
Route::middleware('auth:sanctum')->group(function () {
// Add your protected API routes here
// For example:
Route::get('/user', function (Request $request) {
return $request->user();
});
});
[4] Test
[4.1] Login
Run CURL command:
curl --location 'http://localhost/lara11breeze/public/api/login' `
--header 'Accept: application/json' `
--form '_method="POST"' `
--form 'name="alpha"' `
--form 'email="alpha@razzi.my"' `
--form 'password="your_password"'
Outcome:
The server responded by returning the user information which includes the user_token that can be used in subsequent requests for protected information.
Done.
Download:
https://archive.org/download/laravelprojects/lara11breeze_userapi_20240409.zip
🤓
메타데이터
- post_id
- f8c4e68e650a
- slug
- laravel-11-breeze-auth-api-adding-an-api-route-to-laravel-11-project-f8c4e68e650a
- url
- https://blog.devgenius.io/laravel-11-breeze-auth-api-adding-an-api-route-to-laravel-11-project-f8c4e68e650a
- canonical_url
- https://blog.devgenius.io/laravel-11-breeze-auth-api-adding-an-api-route-to-laravel-11-project-f8c4e68e650a
- author_url
- https://medium.com/@mohamad.razzi.my
- status
- ok
- fetched_at
- 2026-07-19 21:38:15