I have created a REST API with Laravel. How can I use this API in my Next.js application? Is it logical to use Next.js and Laravel together? Will I use it with the Next.js route handler? Will I use it with server actions? I am very confused.
I tried route handler and server action but I couldn’t understand them. What is the correct way to do this?
Selçuk is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
3
Based on your comment:
My backend project is in a separate folder, and my frontend project is in a separate folder. I created them separately
I guess you have to use a route handler and fetch the data from the laravel application. To do this:
-
You have to run the laravel application separately:
// for development you can run the application using the following command in terminal php artisan serve
-
Using a route handler you have to call the backend by using the laravel application’s url and its routes. For example:
const res = await fetch('http://localhost:8000/...', { method: 'POST', headers: { 'Content-Type': 'application/json', 'API-Key': process.env.DATA_API_KEY!, }, body: JSON.stringify({ time: new Date().toISOString() }), })
Sri Harsha is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.