host J
host J

Reputation: 98

Laravel: How to get the json response from API

This is the response returned from one of my controllers

    class initController extends Controller
    {

        public function index(Request $request)
        {

            $init=DB::table('inits')->select('authID')->inRandomOrder()->first();
            if($init == false){
                return response()->json(['status:' => 1 ,'authID' => current($init)]);
            }
            return response()->json(['status:' => 0 ,'authID' => current($init)]);
        }

    }

This is the API route

    Route::get('init', 'initController@index');
    Route::get('getLargeImage', 'getLargeImageController@getImg');

This is the getLargeImage contoller

class getLargeImageController extends Controller
{


    public function index()
    {

    }

}

Now, in this controller how can I get the json response from the first one?

Upvotes: 0

Views: 2898

Answers (2)

BadPiggie
BadPiggie

Reputation: 6379

You can try this,

class getLargeImageController extends Controller
{
    public function index(Request $request)
    {
       $response = app('App\Http\Controllers\initController')->index($request);

    }
}

Upvotes: 0

claireckc
claireckc

Reputation: 455

I think a simple Google search would suffice; https://laravel-tricks.com/tricks/internal-requests

There are many articles on making internal requests on Laravel, and also, this question has been asked and answered previously here, e.g; Consuming my own Laravel API

To answer your question, you make an internal request base on your route;

$request = Request::create('init', 'GET');
$response = Route::dispatch($request);

Upvotes: 1

Related Questions