Laravel: How to get the json response from API











up vote
0
down vote

favorite












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?










share|improve this question






















  • Possible duplicate of Consuming my own Laravel API
    – claireckc
    Nov 22 at 17:24










  • This is not how controllers are meant to be used. You are most likely looking for some sort of service class.
    – Namoshek
    Nov 22 at 17:30















up vote
0
down vote

favorite












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?










share|improve this question






















  • Possible duplicate of Consuming my own Laravel API
    – claireckc
    Nov 22 at 17:24










  • This is not how controllers are meant to be used. You are most likely looking for some sort of service class.
    – Namoshek
    Nov 22 at 17:30













up vote
0
down vote

favorite









up vote
0
down vote

favorite











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?










share|improve this question













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?







php laravel






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 22 at 17:19









host J

265




265












  • Possible duplicate of Consuming my own Laravel API
    – claireckc
    Nov 22 at 17:24










  • This is not how controllers are meant to be used. You are most likely looking for some sort of service class.
    – Namoshek
    Nov 22 at 17:30


















  • Possible duplicate of Consuming my own Laravel API
    – claireckc
    Nov 22 at 17:24










  • This is not how controllers are meant to be used. You are most likely looking for some sort of service class.
    – Namoshek
    Nov 22 at 17:30
















Possible duplicate of Consuming my own Laravel API
– claireckc
Nov 22 at 17:24




Possible duplicate of Consuming my own Laravel API
– claireckc
Nov 22 at 17:24












This is not how controllers are meant to be used. You are most likely looking for some sort of service class.
– Namoshek
Nov 22 at 17:30




This is not how controllers are meant to be used. You are most likely looking for some sort of service class.
– Namoshek
Nov 22 at 17:30












2 Answers
2






active

oldest

votes

















up vote
0
down vote



accepted










You can try this,



class getLargeImageController extends Controller
{
public function index(Request $request)
{
$response = app('AppHttpControllersinitController')->index($request);

}
}





share|improve this answer























  • Thanks, don't understand how to use this.
    – host J
    Nov 22 at 18:07










  • @hostJ I updated...
    – Banujan Balendrakumar
    Nov 22 at 18:08










  • got this error Too few arguments to function AppHttpControllersinitController::index(), 0 passed in /home/jonnyy/PhpstormProjects/zcapt/app/Http/Controllers/getLargeImageController.php on line 20 and exactly 1 expected
    – host J
    Nov 22 at 18:20










  • @hostJ I am really sorry. You should pass the request. Updated plz check
    – Banujan Balendrakumar
    Nov 22 at 18:22












  • @ ban It works for me thank you so much
    – host J
    Nov 22 at 18:38


















up vote
1
down vote













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);





share|improve this answer





















  • how to implement this, I put in my getlargeImage controller and dd($response) it returns an error which is Class 'AppHttpControllersRoute' not found
    – host J
    Nov 22 at 18:00












  • if it is not found, put it in via use AppHttpControllersRoute; at the top of your file where all dependencies are included
    – claireckc
    Nov 22 at 18:10












  • I tried but still not work, seems like the Route::dispatch instance is not recommended in the latest ver(5.7), they prefer 'app()->handle()' method which you mentioned in the consuming-my-own-laravel-api
    – host J
    Nov 22 at 18:27













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53435741%2flaravel-how-to-get-the-json-response-from-api%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote



accepted










You can try this,



class getLargeImageController extends Controller
{
public function index(Request $request)
{
$response = app('AppHttpControllersinitController')->index($request);

}
}





share|improve this answer























  • Thanks, don't understand how to use this.
    – host J
    Nov 22 at 18:07










  • @hostJ I updated...
    – Banujan Balendrakumar
    Nov 22 at 18:08










  • got this error Too few arguments to function AppHttpControllersinitController::index(), 0 passed in /home/jonnyy/PhpstormProjects/zcapt/app/Http/Controllers/getLargeImageController.php on line 20 and exactly 1 expected
    – host J
    Nov 22 at 18:20










  • @hostJ I am really sorry. You should pass the request. Updated plz check
    – Banujan Balendrakumar
    Nov 22 at 18:22












  • @ ban It works for me thank you so much
    – host J
    Nov 22 at 18:38















up vote
0
down vote



accepted










You can try this,



class getLargeImageController extends Controller
{
public function index(Request $request)
{
$response = app('AppHttpControllersinitController')->index($request);

}
}





share|improve this answer























  • Thanks, don't understand how to use this.
    – host J
    Nov 22 at 18:07










  • @hostJ I updated...
    – Banujan Balendrakumar
    Nov 22 at 18:08










  • got this error Too few arguments to function AppHttpControllersinitController::index(), 0 passed in /home/jonnyy/PhpstormProjects/zcapt/app/Http/Controllers/getLargeImageController.php on line 20 and exactly 1 expected
    – host J
    Nov 22 at 18:20










  • @hostJ I am really sorry. You should pass the request. Updated plz check
    – Banujan Balendrakumar
    Nov 22 at 18:22












  • @ ban It works for me thank you so much
    – host J
    Nov 22 at 18:38













up vote
0
down vote



accepted







up vote
0
down vote



accepted






You can try this,



class getLargeImageController extends Controller
{
public function index(Request $request)
{
$response = app('AppHttpControllersinitController')->index($request);

}
}





share|improve this answer














You can try this,



class getLargeImageController extends Controller
{
public function index(Request $request)
{
$response = app('AppHttpControllersinitController')->index($request);

}
}






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 22 at 18:22

























answered Nov 22 at 17:24









Banujan Balendrakumar

477210




477210












  • Thanks, don't understand how to use this.
    – host J
    Nov 22 at 18:07










  • @hostJ I updated...
    – Banujan Balendrakumar
    Nov 22 at 18:08










  • got this error Too few arguments to function AppHttpControllersinitController::index(), 0 passed in /home/jonnyy/PhpstormProjects/zcapt/app/Http/Controllers/getLargeImageController.php on line 20 and exactly 1 expected
    – host J
    Nov 22 at 18:20










  • @hostJ I am really sorry. You should pass the request. Updated plz check
    – Banujan Balendrakumar
    Nov 22 at 18:22












  • @ ban It works for me thank you so much
    – host J
    Nov 22 at 18:38


















  • Thanks, don't understand how to use this.
    – host J
    Nov 22 at 18:07










  • @hostJ I updated...
    – Banujan Balendrakumar
    Nov 22 at 18:08










  • got this error Too few arguments to function AppHttpControllersinitController::index(), 0 passed in /home/jonnyy/PhpstormProjects/zcapt/app/Http/Controllers/getLargeImageController.php on line 20 and exactly 1 expected
    – host J
    Nov 22 at 18:20










  • @hostJ I am really sorry. You should pass the request. Updated plz check
    – Banujan Balendrakumar
    Nov 22 at 18:22












  • @ ban It works for me thank you so much
    – host J
    Nov 22 at 18:38
















Thanks, don't understand how to use this.
– host J
Nov 22 at 18:07




Thanks, don't understand how to use this.
– host J
Nov 22 at 18:07












@hostJ I updated...
– Banujan Balendrakumar
Nov 22 at 18:08




@hostJ I updated...
– Banujan Balendrakumar
Nov 22 at 18:08












got this error Too few arguments to function AppHttpControllersinitController::index(), 0 passed in /home/jonnyy/PhpstormProjects/zcapt/app/Http/Controllers/getLargeImageController.php on line 20 and exactly 1 expected
– host J
Nov 22 at 18:20




got this error Too few arguments to function AppHttpControllersinitController::index(), 0 passed in /home/jonnyy/PhpstormProjects/zcapt/app/Http/Controllers/getLargeImageController.php on line 20 and exactly 1 expected
– host J
Nov 22 at 18:20












@hostJ I am really sorry. You should pass the request. Updated plz check
– Banujan Balendrakumar
Nov 22 at 18:22






@hostJ I am really sorry. You should pass the request. Updated plz check
– Banujan Balendrakumar
Nov 22 at 18:22














@ ban It works for me thank you so much
– host J
Nov 22 at 18:38




@ ban It works for me thank you so much
– host J
Nov 22 at 18:38












up vote
1
down vote













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);





share|improve this answer





















  • how to implement this, I put in my getlargeImage controller and dd($response) it returns an error which is Class 'AppHttpControllersRoute' not found
    – host J
    Nov 22 at 18:00












  • if it is not found, put it in via use AppHttpControllersRoute; at the top of your file where all dependencies are included
    – claireckc
    Nov 22 at 18:10












  • I tried but still not work, seems like the Route::dispatch instance is not recommended in the latest ver(5.7), they prefer 'app()->handle()' method which you mentioned in the consuming-my-own-laravel-api
    – host J
    Nov 22 at 18:27

















up vote
1
down vote













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);





share|improve this answer





















  • how to implement this, I put in my getlargeImage controller and dd($response) it returns an error which is Class 'AppHttpControllersRoute' not found
    – host J
    Nov 22 at 18:00












  • if it is not found, put it in via use AppHttpControllersRoute; at the top of your file where all dependencies are included
    – claireckc
    Nov 22 at 18:10












  • I tried but still not work, seems like the Route::dispatch instance is not recommended in the latest ver(5.7), they prefer 'app()->handle()' method which you mentioned in the consuming-my-own-laravel-api
    – host J
    Nov 22 at 18:27















up vote
1
down vote










up vote
1
down vote









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);





share|improve this answer












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);






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 22 at 17:29









claireckc

907




907












  • how to implement this, I put in my getlargeImage controller and dd($response) it returns an error which is Class 'AppHttpControllersRoute' not found
    – host J
    Nov 22 at 18:00












  • if it is not found, put it in via use AppHttpControllersRoute; at the top of your file where all dependencies are included
    – claireckc
    Nov 22 at 18:10












  • I tried but still not work, seems like the Route::dispatch instance is not recommended in the latest ver(5.7), they prefer 'app()->handle()' method which you mentioned in the consuming-my-own-laravel-api
    – host J
    Nov 22 at 18:27




















  • how to implement this, I put in my getlargeImage controller and dd($response) it returns an error which is Class 'AppHttpControllersRoute' not found
    – host J
    Nov 22 at 18:00












  • if it is not found, put it in via use AppHttpControllersRoute; at the top of your file where all dependencies are included
    – claireckc
    Nov 22 at 18:10












  • I tried but still not work, seems like the Route::dispatch instance is not recommended in the latest ver(5.7), they prefer 'app()->handle()' method which you mentioned in the consuming-my-own-laravel-api
    – host J
    Nov 22 at 18:27


















how to implement this, I put in my getlargeImage controller and dd($response) it returns an error which is Class 'AppHttpControllersRoute' not found
– host J
Nov 22 at 18:00






how to implement this, I put in my getlargeImage controller and dd($response) it returns an error which is Class 'AppHttpControllersRoute' not found
– host J
Nov 22 at 18:00














if it is not found, put it in via use AppHttpControllersRoute; at the top of your file where all dependencies are included
– claireckc
Nov 22 at 18:10






if it is not found, put it in via use AppHttpControllersRoute; at the top of your file where all dependencies are included
– claireckc
Nov 22 at 18:10














I tried but still not work, seems like the Route::dispatch instance is not recommended in the latest ver(5.7), they prefer 'app()->handle()' method which you mentioned in the consuming-my-own-laravel-api
– host J
Nov 22 at 18:27






I tried but still not work, seems like the Route::dispatch instance is not recommended in the latest ver(5.7), they prefer 'app()->handle()' method which you mentioned in the consuming-my-own-laravel-api
– host J
Nov 22 at 18:27




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53435741%2flaravel-how-to-get-the-json-response-from-api%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

What visual should I use to simply compare current year value vs last year in Power BI desktop

Alexandru Averescu

Trompette piccolo