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

luddinus's avatar

What do you think about this approach? ("Internal" Api Requests returning HTML)

Hi.

Imagine I have a web application where a list of recent users are shown. So in my routes/web.php I would have something like this (nothing new, basic approach):

routes file

Route::get('users/recent', [RecentUsersController::class, 'index']);

controller

class RecentUsersController
{
    public function index()
	{
	   $recentUsers = User::recent()->get(); // scopeRecent
	   
	   return view('pages.recent-users', compact('recentUsers'));
	}
}

views/pages/recent-users.blade.php

// navbar etc.

// content...
    @include('partials.recent-users')
// footer

So my idea is to make something like this:

// routes file

Route::view('recent-users', 'pages.recent-users');
Route::get('api/users/recent', [RecentUsersController::class, 'index']);

// controller

class RecentUsersController
{
   public function index()
   {
      $recentUsers = User::recent()->get();

      if (request()->wantsJson()) {
	      return $recentUsers->toJson();
	  }
	  
	  return view('partials.recent-users', compact('recentUsers'));
   }
}

// pages.recent-users

// there is no $recentUsers variable available in the view so this is the approach Im talking about
// instead of using "include", make an "internal request" which returns either HTML or json (depending on the case)
// in this case it returns the html I want (the partial view rendered)

// navbar etc.
// content
   <div class="container">
      {!! Http::get('/api/users/recent') !!}
   </div>

What do you think?

0 likes
8 replies
luddinus's avatar

Yes I know about laravel-livewire.

In fact, this "idea" I get from one of the videos that Caleb Porzio uploaded to laracasts.

topvillas's avatar

There is another open source library that github use themselves but I can't remember it's name or whether it's just for Rails.

topvillas's avatar

That's the kiddie! Might be useful if Livewire is a bit more than you need.

martinbean's avatar

@luddinus So instead of your controller fetching the records it needs directly from the database and displaying them in a Blade template, you want to make a HTTP request to a controller that makes another HTTP request to your API that then fetches the records you need from the database…?

luddinus's avatar

@martinbean Yes.

It sounds weird but this way I can "reuse" the API when a mobile app is made (maybe never, lol)

martinbean's avatar
Level 80

@luddinus But you’re essentially doubling your request time because now two HTTP requests need to be made to get data: one to your application, and then one your application makes to your API.

I can "reuse" the API when a mobile app is made (maybe never, lol)

YAGNI. Build it when you need it. There’s nothing stopping your building a “traditional” server-rendered application that fetches your data directly from the database, and then exposing an API if you do offer a mobile app. Don’t make bizarre, inefficient architectural decisions based on some “maybe” event.

1 like

Please or to participate in this conversation.