问题
Before starting
I use Laravel as example, but I actually made a small project with hand-made MVC and the issue is still occuring, so this has nothing to do with the framework.
Context
I use php artisan serve
, which is the equivalent of php -S localhost:8000 -t public
to boot up my web app. I do not use Apache (might be a hint ?).
The client (human) request the page /user
through his web browser. Here is how the controller that responds to this endpoint looks like:
routes.php
<?php
Route::resource('user', 'UserController');
?>
UserController.php
<?php
namspace App\Http\Controller;
use Illuminate\Http\Request;
use App\User;
class UserController {
public function index( Request $request ) {
$users = User::all();
if( $request->wantsJson() ) {
return response()->json($users, 200);
}
else {
return view('user.index')->with('users', $users);
}
}
}
?>
Nothing wrong on this code, everything works.
I would like to set up a "store" mecanism, where only my api endpoint is responsible of providing the data. So here is the changes:
api.php
<?php
Route::group(['prefix' => 'v1'], function() {
Route::resource('user', 'UserApiController');
});
?>
UserApiController.php
<?php
namespace App\Http\Controller;
use Illuminate\Http\Request;
use App\User;
class UserApiController {
public function index() {
return response()->json(User::all(), 200);
}
}
?>
UserController.php
<?php
namespace App\Http\Controller;
use Illuminate\Http\Request;
use App\User;
class UserController {
public function index() {
return view('user.index')->with('users', User::all());
}
}
?>
To reproduce the issue
To do so, correct me if I go on the wrong path, my endpoint /user should request the data from /api/v1/user, and then provide the data to the view and return the Http response. So here is what I modifyied on the UserController.php
:
UserController.php
<?php
namespace App\Http\Controller;
use Illuminate\Http\Request;
use App\User;
use GuzzleHttp\Client;
class UserController {
public function index() {
$client = new Client;
$response = $client->get(url('/api/v1/user'));
return view('user.index')->with('users', User::all());
}
}
?>
For me, the endpoint will receive the request from the client (human), then send a second request to the api endpoint, fetch the json data, parse it (using $data = $response->getBody()->getContents();
GuzzleHttp method), and then provide it to the view (using return view('user.index')->with('users', $data);
.
Issue
The server actually infinite loop and I do not understand what is failing in the process. Note that calling an api from another server (for example https://ipapi.co/json
works totally fine).
Can someone give me a hint on what is causing this freeze?
回答1:
This is definitively related to the way PHP handles requests. I rooted my project on same location localhost:8000
by setting Apache configuration httpd.conf
and it is working, I can make requests across my routes.
If any kind soul could paste here a technical detail on what is happening with my requests on PHP web server...
来源:https://stackoverflow.com/questions/48433296/calling-route-from-same-server-causes-an-infinite-loop