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

mvieira's avatar

How a Controller consumes API from another controller?

I have these endpoint to consume in my controller. How do I consume them without needing to GuzzleHttp or Curl ?

I am doing this ( as an example ) :

$wean_data = app('App\Http\Controllers\Api\AnimalWeaningsApiController')->assignAllCalculations($request);

My problem here is the $request, since all APIs are expecting a ( Request $request ), I cannot in my current controller do this :

$params = $request->all(); $wean_data = app('App\Http\Controllers\Api\AnimalWeaningsApiController')->assignAllCalculations($request);

I want to process the $params, before each api and after I call them, meaning,

$params = $request->all(); // do something with $params

// consume API $params = app('App\Http\Controllers\Api\AnimalWeaningsApiController')->assignAllCalculations($params);

// do something with $params // comsume the second api, do something with the data and again call the third.

Route::post('weaning-all-calculations/assign', 'AnimalWeaningsApiController@assignAllCalculations'); Route::post('yearling-all-calculations/assign', 'AnimalYearlingsApiController@assignAllCalculations'); Route::post('ultrasound-all-calculations/assign', 'AnimalUltrasoundsApiController@assignAllCalculations');

0 likes
9 replies
martinbean's avatar

@mvieira You are going about this completely the wrong way. I don’t think you understand what an API is. If you did, you wouldn’t be trying to instantiate a controller from another. That’s yet another anti-pattern.

Extract the logic in your API controller to a separate class. You can then use that class in your non-API controller instead of trying to call one controller from another.

1 like
mvieira's avatar

I don't want to instantiate a controler from another, the idea is to consume the return of those 3 apis. If I get the logic of the api to a separate class I will be duplicating the code which I also want to avoid.

The example above is to give an idea how I wish to work with apis that already exist in which I would like to use in the current controller.

If you have any other suggestion please do share , thanks. 8-)

martinbean's avatar

I don't want to instantiate a controler from another

@mvieira Then what is this all about?

$wean_data = app('App\Http\Controllers\Api\AnimalWeaningsApiController')->assignAllCalculations($request);

That’s instantiating a controller, presumably in another controller.

If I get the logic of the api to a separate class I will be duplicating the code which I also want to avoid.

No, you wouldn’t? You would be doing the exact opposite. You extract the logic to a class that you can use in both your API and web controllers. That’s not duplication; that’s abstraction.

So, if the API is a third-party controller, use a HTTP client to make requests:

$response = Http::get('http://example.com/api/foo-endpoint');

If the API controllers are in the same project as the web controller, then extract the logic from the API controllers so you can use the same logic in your web controllers as well.

sr57's avatar

If you have any other suggestion please do share

Learn how to test your api first

If you have any other suggestion please do share

and than adapt what you want.

mvieira's avatar

Yes I am getting json data.

I am still reading the code, since it is from a laravel version before 5.6 and there are many problems and I am trying to no include more..hehe.

The main idea is just to know if there is a way to consume the apis that were already created. As martinbean suggested, I could create separate classes with the logic, but before that I was asking if is it OK or possible to consume those APIs previous created, since one of the apis has a weird logic and trying to extract could be a little troublesome.

I am only working with backend, so Ajax I don't think it would do it.

But in short I receive a Json and than I will "do something" with that data, depending on the situation I will call one or all apis in a certain order and each time I will have to "do something" after I get the response.

If there isn't a way, I am ok with it, I just didn't know some people would be toxic about it. ( The guy that deleted his response ).

jlrdw's avatar

@mvieira I had referred to another post, but since it was guzzle I deleted. And just curious, what was toxic?

I only asked to please format your code, I even included please. And yes I stated ajax as an option. But if it won't work for you that's fine also.

1 like
mvieira's avatar

Thanks man, it was not you, someone was spamming message then I noticed that he deleted and for some reason my message went to you. The same guy was spamming and deleting from other threads as well, I think that's how he raise his forum points..hehe.

I have taken martinbean suggestion...I asked the people here to allow me to fix some of this legacy code by putting in a separate class. I think thats the best course. Thanks.

jlrdw's avatar

@martinbean knows that api stuff well, I have only used a couple but been a while, one was adopt a pet.

@mvieira And thanks for clarifying.

mvieira's avatar

We use api all the time but this was a first from a controller...so I had to ask...hehehe.

Please or to participate in this conversation.