从同一服务器调用路由会导致无限循环 [英] Calling route from same server causes an infinite loop

查看:68
本文介绍了从同一服务器调用路由会导致无限循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

开始之前

我以Laravel为例,但实际上我是用手工制作的MVC做的一个小项目,问题仍然存在,所以与框架无关.

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.

上下文

我使用php artisan serve,相当于php -S localhost:8000 -t public来启动我的Web应用程序.我不使用Apache(可能是个提示吗?).

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 ?).

客户(人类)通过其Web浏览器请求页面/user.响应该端点的控制器如下所示:

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.

我想建立一个存储"机制,仅由我的api端点负责提供数据.更改如下:

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

?>

重现问题

为此,如果我走了错误的路径,请纠正我,我的端点/user应该从/api/v1/user请求数据,然后将数据提供给视图并返回Http响应.所以这是我在UserController.php上修改的内容:

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());
        }
    }
?>

对于我来说,端点将接收来自客户端(人类)的请求,然后向api端点发送第二个请求,获取json数据,解析(使用$data = $response->getBody()->getContents(); GuzzleHttp方法),然后将其提供给视图(使用return view('user.index')->with('users', $data);.

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

问题

服务器实际上是无限循环的,我不了解该过程中发生了什么故障.请注意,从另一台服务器调用api(例如https://ipapi.co/json可以很好地工作).

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?

推荐答案

这与PHP处理请求的方式绝对相关.我通过设置Apache配置httpd.conf将项目植在相同的位置localhost:8000上,并且它可以正常工作,我可以跨路线发出请求.

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.

如果有什么灵魂可以在这里粘贴有关我在PHP Web服务器上的请求所发生的情况的技术详细信息...

If any kind soul could paste here a technical detail on what is happening with my requests on PHP web server...

这篇关于从同一服务器调用路由会导致无限循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆