How to team Laravel api with Next.Js within same domain name

Initial configuration:

Laravel 7.x folder: /var/www/laravel

Next.Js folder: /var/www/public_html

Step 1. Add your api response to laravel

Add to /var/www/routes/web.php

Route::get('/', [
   'uses' => 'IndexController@categories',
]);

Add to /var/www/app/Http/Controllers/IndexController.php

public function index() {
  return response()->json(['Hello world']);
}

Step 2. Add api/ folder to Next.Js public folder. Don’t touch existing Next.Js static html files

Move /var/www/laravel/public/index.php to /var/www/public_html/api

Then edit it, change the following:

require __DIR__.'/../vendor/autoload.php';
to 
require __DIR__.'/../../laravel/vendor/autoload.php';

$app = require_once __DIR__.'/../bootstrap/app.php';
to
$app = require_once __DIR__.'/../../laravel/bootstrap/app.php';

Move /var/www/laravel/public/.htaccess to /var/www/public_html/api, no need to edit it

Finally

Try to access your domain.com/api. It should work

Leave a Reply