To build the same functionality using Laravel (a PHP framework) for the backend and React for the frontend, the steps and structure change slightly. Below is the breakdown of how to create a Laravel application that syncs data from Printify, displays it, and exports that data to an Etsy store.
- Knowledge Required: PHP, Laravel, JavaScript, React, REST APIs, and basic database management.
- Tools Required: Composer (for Laravel), Node.js, Laravel, React, Axios (for API calls), MySQL or other relational databases, Printify API keys, Etsy API keys.
my-laravel-app/
│
├── app/ # Laravel application logic
│ ├── Http/
│ │ ├── Controllers/ # Controllers for business logic
│ │ ├── Models/ # Eloquent models for database tables
│
├── routes/
│ ├── api.php # API routes for Printify and Etsy integration
│
├── database/ # Migrations and seeders for the database
│
├── resources/ # Frontend resources
│ ├── js/ # React app can live here
│
├── .env # Environment variables (API keys, secrets)
├── composer.json # PHP dependencies
├── package.json # JS dependencies
└── README.md # Project documentation
-
Install Laravel:
composer create-project --prefer-dist laravel/laravel my-laravel-app cd my-laravel-app -
Set Up Environment Variables: Edit the
.envfile with your database configuration:DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=my_database DB_USERNAME=root DB_PASSWORD=your_password PRINTIFY_API_KEY=your_printify_api_key ETSY_API_KEY=your_etsy_api_key
-
Create the Product Model:
php artisan make:model Product -m
This will create a
Productmodel and a migration file. -
Define the
ProductMigration: Edit the migration file indatabase/migrations:public function up() { Schema::create('products', function (Blueprint $table) { $table->id(); $table->string('title'); $table->text('description'); $table->string('printify_id'); $table->decimal('price', 8, 2); $table->string('image_url'); $table->timestamps(); }); }
-
Run the Migration:
php artisan migrate
-
Create the Printify Controller:
php artisan make:controller PrintifyController
-
Create the Etsy Controller:
php artisan make:controller EtsyController
- Add Routes in
routes/api.php:use App\Http\Controllers\PrintifyController; use App\Http\Controllers\EtsyController; Route::get('/printify/sync', [PrintifyController::class, 'sync']); Route::post('/etsy/export', [EtsyController::class, 'export']);
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Product;
use Illuminate\Support\Facades\Http;
class PrintifyController extends Controller
{
public function sync(Request $request)
{
$printifyApiKey = env('PRINTIFY_API_KEY');
$response = Http::withHeaders([
'Authorization' => "Bearer {$printifyApiKey}"
])->get('https://api.printify.com/v1/shops/your-shop-id/products.json');
if ($response->successful()) {
$products = $response->json()['data'];
foreach ($products as $productData) {
Product::updateOrCreate(
['printify_id' => $productData['id']],
[
'title' => $productData['title'],
'description' => $productData['description'],
'price' => $productData['variants'][0]['price'],
'image_url' => $productData['images'][0]['src']
]
);
}
return response()->json(['message' => 'Data synced successfully']);
}
return response()->json(['message' => 'Failed to sync data'], 500);
}
}namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Product;
use Illuminate\Support\Facades\Http;
class EtsyController extends Controller
{
public function export(Request $request)
{
$etsyApiKey = env('ETSY_API_KEY');
$products = Product::all();
$exportedProducts = [];
foreach ($products as $product) {
$response = Http::withHeaders([
'Authorization' => "Bearer {$etsyApiKey}"
])->post('https://openapi.etsy.com/v3/application/listings', [
'title' => $product->title,
'description' => $product->description,
'price' => $product->price,
'images' => [$product->image_url],
]);
if ($response->successful()) {
$exportedProducts[] = $response->json();
}
}
return response()->json(['message' => 'Products exported to Etsy', 'exportedProducts' => $exportedProducts]);
}
}-
Install React in Laravel:
npm install && npm install react react-dom -
Configure React in Laravel: Create a new
App.jsfile inresources/jsand set up the component:import React, { useState } from 'react'; import axios from 'axios'; function App() { const [products, setProducts] = useState([]); const syncPrintify = async () => { try { const response = await axios.get('/api/printify/sync'); alert(response.data.message); } catch (error) { console.error('Error syncing data:', error); } }; const exportToEtsy = async () => { try { const response = await axios.post('/api/etsy/export'); alert(response.data.message); } catch (error) { console.error('Error exporting data:', error); } }; return ( <div> <h1>Printify to Etsy Sync</h1> <button onClick={syncPrintify}>Sync Printify Data</button> <button onClick={exportToEtsy}>Export to Etsy</button> </div> ); } export default App;
-
Compile Frontend:
npm run dev
Start the Laravel server:
php artisan serveVisit the application in the browser at http://localhost:8000, where you can use the sync and export features.
- Replace
"your-shop-id"in the Printify API endpoint with your actual shop ID. - Set up environment variables in
.envfor API keys. - Implement validation and error handling for production.
- Customize the frontend as needed for better user experience.
This setup will provide a Laravel-based backend for handling Printify and Etsy integrations, with a React-based frontend for user interactions.