Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

FloppyCode's avatar

Changed routes from direct calls to Controller instances and the API requests became way slower

So, I'm building a mobile app back-end with Laravel. My first task is to grab a, possibly huge, amount of data from third parties public APIs to populate a basic DB to start with, just even testing.

Before making the changes that slowed my back-end I did organise my routes as it follows:

web.php:

use App\Http\Controllers\Admin\PlacesController as AdminPlacesController;
use App\Http\Controllers\Api\PlacesController as ApiPlacesController;
use App\Http\Controllers\ProfileController;
use Illuminate\Support\Facades\Route;
Route::middleware('auth')
  ->prefix('admin')
  ->name('admin.')
  ->group(function () {
    Route::get('/places', [AdminPlacesController::class, 'api'])->name('api');
    Route::post('places/city-id', [ApiPlacesController::class, 'fetchCityPOIs'])->name('places.cityid');
  });

api.php:

nothing

Api/PlacesController -> fetch and store APIs data methods;

Admin/PlacesController -> just returns view('admin.api').

set up like this my app was damn fast, the requests from third parties APIs took like less than 1 second. Then I changed all as it follows:

web.php:

use App\Http\Controllers\Admin\PlacesController as AdminPlacesController;
use App\Http\Controllers\ProfileController;
use Illuminate\Support\Facades\Route;

Route::middleware('auth')->prefix('admin')->name('admin.')->group(function () {
  Route::get('/places', [AdminPlacesController::class, 'showForm'])->name('places.form');
  Route::post('/places', [AdminPlacesController::class, 'processForm'])->name('places.process');
  Route::get('/places/index', [AdminPlacesController::class, 'index'])->name('places.index');
});

api.php:

use App\Http\Controllers\Api\PlacesController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;

Route::middleware('auth:api')->group(function () {
    Route::post('/places/fetch', [PlacesController::class, 'fetchCityAndPoisApis']);
});

Api/PlaceController -> the same fetch and store APIs data methods as before;

Admin/PlacesController ->

use App\Http\Controllers\Controller;
use App\Http\Requests\Auth\PlaceUpsertRequest;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Api\PlacesController as ApiPlacesController;

class PlacesController extends Controller
{
  public function showForm()
  {
    return view('admin.places');
  }

  public function processForm(PlaceUpsertRequest $request)
  {
    // Pass the request object to the fetchCityPois method
    $apiController = new ApiPlacesController();
    $apiController->fetchCityAndPoisApis($request);
    
    // Optionally, you can redirect the user to another page after form processing
    return redirect()->route('admin.places.index');
  }

  public function index()
  {
    $result = DB::table('cities')
      ->join('places', 'cities.id', '=', 'places.city_id')
      ->select('cities.name_it as city_name', 'places.*')
      ->orderBy('cities.id')
      ->get();

    $citiesData = [];

    foreach ($result as $place) {
      $cityName = $place->city_name;
      unset($place->city_name);
      $citiesData[$cityName][] = $place;
    }

    return view('admin.places.index', compact('citiesData'));
  }
}

Do you see any reason to why the whole fetch and store and return the index.blade.php should be 5/6 seconds slower, compared to the 1/1.5 seconds response time it was before? Is it because I'm instanciating a Controller instead of calling the method or what?

0 likes
0 replies

Please or to participate in this conversation.