Lädt...

🔧 How to Build an API with Laravel Breeze in Laravel 11


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

How to Build an API with Laravel Breeze in Laravel 11

A step-by-step guide on building a simple API with authentication using Laravel Breeze in Laravel 11.

Step 1: Install Laravel

First, create a new Laravel project using the Laravel installer or Composer.

laravel new api-breeze
# Or via Composer
composer create-project laravel/laravel api-breeze
cd api-breeze

Step 2: Install Laravel Breeze

Next, install Laravel Breeze and its dependencies.

composer require laravel/breeze --dev
php artisan breeze:install api

This command will install Breeze and set up the necessary scaffolding for API authentication.

Step 3: Configure the Database and Run Migrations

  1. Update your .env file with your database credentials:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel11_api
DB_USERNAME=root
DB_PASSWORD=
  1. Run the migrations to set up your database tables:
php artisan migrate

Step 4: Create Authentication Endpoints

Laravel Breeze provides the necessary endpoints for registration, login, and logout. The routes are defined in routes/api.php.

use App\Http\Controllers\Auth\AuthenticatedSessionController;
use App\Http\Controllers\Auth\RegisteredUserController;
use Illuminate\Support\Facades\Route;

Route::post('/register', [RegisteredUserController::class, 'store']);
Route::post('/login', [AuthenticatedSessionController::class, 'store']);
Route::post('/logout', [AuthenticatedSessionController::class, 'destroy'])->middleware('auth:sanctum');

Step 5: Update Controllers

Modify the RegisteredUserController and AuthenticatedSessionController to return JSON responses.

RegisteredUserController.php

namespace App\Http\Controllers\Auth;

use App\Models\User;
use Illuminate\Auth\Events\Registered;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Validation\Rules;
use App\Http\Controllers\Controller;

class RegisteredUserController extends Controller
{
    public function store(Request $request)
    {
        $request->validate([
            'name' => ['required', 'string', 'max:255'],
            'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
            'password' => ['required', 'confirmed', Rules\Password::defaults()],
        ]);

        $user = User::create([
            'name' => $request->name,
            'email' => $request->email,
            'password' => Hash::make($request->password),
        ]);

        event(new Registered($user));

        $token = $user->createToken('auth_token')->plainTextToken;

        return response()->json([
            'access_token' => $token,
            'token_type' => 'Bearer',
            'user' => $user
        ]);
    }
}

AuthenticatedSessionController.php

namespace App\Http\Controllers\Auth;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Http\Controllers\Controller;

class AuthenticatedSessionController extends Controller
{
    public function store(Request $request)
    {
        $request->validate([
            'email' => ['required', 'string', 'email'],
            'password' => ['required', 'string'],
        ]);

        if (!Auth::attempt($request->only('email', 'password'))) {
            return response()->json(['message' => 'Invalid login credentials'], 401);
        }

        $user = Auth::user();
        $token = $user->createToken('auth_token')->plainTextToken;

        return response()->json([
            'access_token' => $token,
            'token_type' => 'Bearer',
            'user' => $user,
            'status' => 'Login successful',
        ]);
    }

    public function destroy(Request $request)
    {
        $request->user()->currentAccessToken()->delete();

        return response()->json(['message' => 'Logout successful']);
    }
}

Step 5: Run Laravel App

php artisan serve

Step 6: Check following API

Test Your API with Thunder Client

Register

Login

...

🔧 How to Build an API with Laravel Breeze in Laravel 11


📈 41.76 Punkte
🔧 Programmierung

🎥 Using Laravel Breeze Starter Kits in Laravel 12 #laravel #thecodeholic #laravel12


📈 39.46 Punkte
🎥 Video | Youtube

⚠️ [local] Sync Breeze Enterprise 12.4.18 - 'Sync Breeze Enterprise' Unquoted Service Path


📈 33.35 Punkte
⚠️ PoC

⚠️ Sync Breeze 13.6.18 Sync Breeze 13.6.18 Unquoted Service Path


📈 33.35 Punkte
⚠️ PoC

🔧 Set up Laravel authentication using Laravel Breeze


📈 31.86 Punkte
🔧 Programmierung

🔧 Seamless Integration of Laravel Breeze API Scaffolding with React Applications


📈 29.36 Punkte
🔧 Programmierung

🔧 Simplifying API responses in Laravel with kolirt/laravel-api-response package


📈 25.38 Punkte
🔧 Programmierung

🔧 🔐 Setting Up Login & Register in Laravel with Breeze


📈 24.27 Punkte
🔧 Programmierung

🔧 🔥 2 - Laravel Breeze: Easy Authentication Setup (Login & Register)


📈 24.27 Punkte
🔧 Programmierung

🔧 Laravel 12 Removed Breeze and Jetstream from the Installer—So I Built LaravelFS 🚀


📈 24.27 Punkte
🔧 Programmierung

🔧 Laravel Breeze


📈 24.27 Punkte
🔧 Programmierung

🔧 Laravel Breeze


📈 24.27 Punkte
🔧 Programmierung

🔧 Deploying a Breeze Laravel app on Render - part 2


📈 24.27 Punkte
🔧 Programmierung

🔧 Deploying a Breeze Laravel app on Render - part 1


📈 24.27 Punkte
🔧 Programmierung

🔧 Laravel Breeze Change Redirect After Login/Register


📈 24.27 Punkte
🔧 Programmierung

🔧 Laravel Livewire CRUD with Breeze & Tailwind CSS


📈 24.27 Punkte
🔧 Programmierung

🔧 🔐 Using a Custom Model for Laravel Authentication(Breeze) (e.g. Guest Instead of User)


📈 24.27 Punkte
🔧 Programmierung

🎥 Upgrade Laravel 11 to Laravel 12 #laravel #thecodeholic


📈 22.78 Punkte
🎥 Video | Youtube

🎥 Print SQL Queries in Laravel #laravel #thecodeholic #laravelcourse #php #laravel


📈 22.78 Punkte
🎥 Video | Youtube

🎥 Create First Laravel Project using Laravel Herd #laravel #laravelproject #laravelphp


📈 22.78 Punkte
🎥 Video | Youtube

🔧 [Laravel v11 x Docker] Efficiently Set Up a Laravel App Dev Environment with Laravel Sail


📈 22.78 Punkte
🔧 Programmierung

🔧 Laravel API Tutorial: Build, Document, and Secure a REST API


📈 22.59 Punkte
🔧 Programmierung

🔧 Create Mock API Like a Breeze!


📈 21.77 Punkte
🔧 Programmierung

🪟 New Windows 11 Build makes sharing a breeze, lets you email yourself more easily


📈 21.48 Punkte
🪟 Windows Tipps

📰 KDE Frameworks Now Requires Qt 5.5 or Later, Build 5.25.0 Updates Breeze Icons


📈 21.48 Punkte
📰 IT Security

📰 KDE Frameworks Now Requires Qt 5.5 or Later, Build 5.25.0 Updates Breeze Icons


📈 21.48 Punkte
📰 IT Security

🔧 DevLog 20250519: AWS API Gateway Note - HTTP API vs Rest API for API Type


📈 20.38 Punkte
🔧 Programmierung

🔧 Laravel PWA Master Guide: Build App-Like Experiences with Laravel in 5 Minutes


📈 19.99 Punkte
🔧 Programmierung

🎥 Build and Deploy Real Time Messaging App with Laravel and React (with Laravel Reverb)


📈 19.99 Punkte
🎥 Video | Youtube