DEV Community

Sayuri Kotikawaththa
Sayuri Kotikawaththa

Posted on

🌐 The Architecture of a Decoupled Application

Connecting a React Frontend to a Laravel Backend

When you connect a React frontend to a Laravel backend, they act as two separate applications. Laravel serves as the API (Application Programming Interface) server, handling business logic, database operations, and data processing. React serves as the presentation layer, requesting and displaying data from the Laravel API.

Step-by-Step Integration Guide

Step 1: Prepare the Laravel Backend (API Server)

1. Create an API Route

Open routes/api.php and define an API endpoint:

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ProjectController;

Route::get('/projects', [ProjectController::class, 'index']);
Enter fullscreen mode Exit fullscreen mode

2. Create the Controller

Generate a controller:

php artisan make:controller ProjectController
Enter fullscreen mode Exit fullscreen mode

Open app/Http/Controllers/ProjectController.php and add:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\JsonResponse;

class ProjectController extends Controller
{
    public function index(): JsonResponse
    {
        $projects = [
            'Web App',
            'Mobile E-Commerce',
            'Portfolio'
        ];

        return response()->json($projects);
    }
}
Enter fullscreen mode Exit fullscreen mode

3. Configure CORS

Open config/cors.php and ensure your React application's URL is allowed:

'allowed_origins' => [
    'http://localhost:3000',
],
Enter fullscreen mode Exit fullscreen mode

If using Vite React:

'allowed_origins' => [
    'http://localhost:5173',
],
Enter fullscreen mode Exit fullscreen mode

Start the Laravel server:

php artisan serve
Enter fullscreen mode Exit fullscreen mode

Laravel API endpoint:

http://127.0.0.1:8000/api/projects
Enter fullscreen mode Exit fullscreen mode

Step 2: Connect the React Frontend

1. Install Axios

Navigate to your React project directory and run:

npm install axios
Enter fullscreen mode Exit fullscreen mode

2. Create a Component

Create ProjectList.jsx:

import React, { useState, useEffect } from 'react';
import axios from 'axios';

function ProjectList() {
    const [projects, setProjects] = useState([]);

    useEffect(() => {
        axios
            .get('http://127.0.0.1:8000/api/projects')
            .then((response) => {
                setProjects(response.data);
            })
            .catch((error) => {
                console.error('Error fetching projects:', error);
            });
    }, []);

    return (
        <div>
            <h2>Projects List</h2>

            <ul>
                {projects.map((project, index) => (
                    <li key={index}>{project}</li>
                ))}
            </ul>
        </div>
    );
}

export default ProjectList;
Enter fullscreen mode Exit fullscreen mode

3. Use the Component

Open App.jsx:

import React from 'react';
import ProjectList from './ProjectList';

function App() {
    return (
        <div>
            <h1>Laravel + React Integration Example</h1>
            <ProjectList />
        </div>
    );
}

export default App;
Enter fullscreen mode Exit fullscreen mode

4. Start the React Application

npm run dev
Enter fullscreen mode Exit fullscreen mode

or

npm start
Enter fullscreen mode Exit fullscreen mode

depending on your React setup.


Data Flow

React Frontend
       β”‚
       β”‚ HTTP GET Request
       β–Ό
Laravel API
       β”‚
       β”‚ Fetch Data
       β–Ό
Database / Data Source
       β”‚
       β”‚ JSON Response
       β–Ό
React Frontend
       β”‚
       β–Ό
Display Data
Enter fullscreen mode Exit fullscreen mode

Expected Output

Laravel + React Integration Example

Projects List

β€’ Web App
β€’ Mobile E-Commerce
β€’ Portfolio
Enter fullscreen mode Exit fullscreen mode

This architecture keeps the frontend (React) and backend (Laravel) independent, making the application easier to maintain, scale, and develop.

Top comments (0)